130f712c9SDave Chinner /* 230f712c9SDave Chinner * Copyright (c) 2000-2005 Silicon Graphics, Inc. 330f712c9SDave Chinner * Copyright (c) 2013 Red Hat, Inc. 430f712c9SDave Chinner * All Rights Reserved. 530f712c9SDave Chinner * 630f712c9SDave Chinner * This program is free software; you can redistribute it and/or 730f712c9SDave Chinner * modify it under the terms of the GNU General Public License as 830f712c9SDave Chinner * published by the Free Software Foundation. 930f712c9SDave Chinner * 1030f712c9SDave Chinner * This program is distributed in the hope that it would be useful, 1130f712c9SDave Chinner * but WITHOUT ANY WARRANTY; without even the implied warranty of 1230f712c9SDave Chinner * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1330f712c9SDave Chinner * GNU General Public License for more details. 1430f712c9SDave Chinner * 1530f712c9SDave Chinner * You should have received a copy of the GNU General Public License 1630f712c9SDave Chinner * along with this program; if not, write the Free Software Foundation, 1730f712c9SDave Chinner * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 1830f712c9SDave Chinner */ 1930f712c9SDave Chinner #include "xfs.h" 2030f712c9SDave Chinner #include "xfs_fs.h" 2130f712c9SDave Chinner #include "xfs_format.h" 2230f712c9SDave Chinner #include "xfs_log_format.h" 2330f712c9SDave Chinner #include "xfs_trans_resv.h" 2430f712c9SDave Chinner #include "xfs_sb.h" 2530f712c9SDave Chinner #include "xfs_ag.h" 2630f712c9SDave Chinner #include "xfs_mount.h" 2730f712c9SDave Chinner #include "xfs_da_format.h" 2830f712c9SDave Chinner #include "xfs_da_btree.h" 2930f712c9SDave Chinner #include "xfs_inode.h" 3030f712c9SDave Chinner #include "xfs_bmap.h" 3130f712c9SDave Chinner #include "xfs_dir2.h" 3230f712c9SDave Chinner #include "xfs_dir2_priv.h" 3330f712c9SDave Chinner #include "xfs_error.h" 3430f712c9SDave Chinner #include "xfs_trace.h" 3530f712c9SDave Chinner #include "xfs_trans.h" 3630f712c9SDave Chinner #include "xfs_buf_item.h" 3730f712c9SDave Chinner #include "xfs_cksum.h" 3830f712c9SDave Chinner 3930f712c9SDave Chinner /* 4030f712c9SDave Chinner * Function declarations. 4130f712c9SDave Chinner */ 4230f712c9SDave Chinner static int xfs_dir2_leafn_add(struct xfs_buf *bp, xfs_da_args_t *args, 4330f712c9SDave Chinner int index); 4430f712c9SDave Chinner static void xfs_dir2_leafn_rebalance(xfs_da_state_t *state, 4530f712c9SDave Chinner xfs_da_state_blk_t *blk1, 4630f712c9SDave Chinner xfs_da_state_blk_t *blk2); 4730f712c9SDave Chinner static int xfs_dir2_leafn_remove(xfs_da_args_t *args, struct xfs_buf *bp, 4830f712c9SDave Chinner int index, xfs_da_state_blk_t *dblk, 4930f712c9SDave Chinner int *rval); 5030f712c9SDave Chinner static int xfs_dir2_node_addname_int(xfs_da_args_t *args, 5130f712c9SDave Chinner xfs_da_state_blk_t *fblk); 5230f712c9SDave Chinner 5330f712c9SDave Chinner /* 5430f712c9SDave Chinner * Check internal consistency of a leafn block. 5530f712c9SDave Chinner */ 5630f712c9SDave Chinner #ifdef DEBUG 5730f712c9SDave Chinner #define xfs_dir3_leaf_check(dp, bp) \ 5830f712c9SDave Chinner do { \ 5930f712c9SDave Chinner if (!xfs_dir3_leafn_check((dp), (bp))) \ 6030f712c9SDave Chinner ASSERT(0); \ 6130f712c9SDave Chinner } while (0); 6230f712c9SDave Chinner 6330f712c9SDave Chinner static bool 6430f712c9SDave Chinner xfs_dir3_leafn_check( 6530f712c9SDave Chinner struct xfs_inode *dp, 6630f712c9SDave Chinner struct xfs_buf *bp) 6730f712c9SDave Chinner { 6830f712c9SDave Chinner struct xfs_dir2_leaf *leaf = bp->b_addr; 6930f712c9SDave Chinner struct xfs_dir3_icleaf_hdr leafhdr; 7030f712c9SDave Chinner 7130f712c9SDave Chinner dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf); 7230f712c9SDave Chinner 7330f712c9SDave Chinner if (leafhdr.magic == XFS_DIR3_LEAFN_MAGIC) { 7430f712c9SDave Chinner struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr; 7530f712c9SDave Chinner if (be64_to_cpu(leaf3->info.blkno) != bp->b_bn) 7630f712c9SDave Chinner return false; 7730f712c9SDave Chinner } else if (leafhdr.magic != XFS_DIR2_LEAFN_MAGIC) 7830f712c9SDave Chinner return false; 7930f712c9SDave Chinner 8030f712c9SDave Chinner return xfs_dir3_leaf_check_int(dp->i_mount, dp, &leafhdr, leaf); 8130f712c9SDave Chinner } 8230f712c9SDave Chinner #else 8330f712c9SDave Chinner #define xfs_dir3_leaf_check(dp, bp) 8430f712c9SDave Chinner #endif 8530f712c9SDave Chinner 8630f712c9SDave Chinner static bool 8730f712c9SDave Chinner xfs_dir3_free_verify( 8830f712c9SDave Chinner struct xfs_buf *bp) 8930f712c9SDave Chinner { 9030f712c9SDave Chinner struct xfs_mount *mp = bp->b_target->bt_mount; 9130f712c9SDave Chinner struct xfs_dir2_free_hdr *hdr = bp->b_addr; 9230f712c9SDave Chinner 9330f712c9SDave Chinner if (xfs_sb_version_hascrc(&mp->m_sb)) { 9430f712c9SDave Chinner struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr; 9530f712c9SDave Chinner 9630f712c9SDave Chinner if (hdr3->magic != cpu_to_be32(XFS_DIR3_FREE_MAGIC)) 9730f712c9SDave Chinner return false; 9830f712c9SDave Chinner if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_uuid)) 9930f712c9SDave Chinner return false; 10030f712c9SDave Chinner if (be64_to_cpu(hdr3->blkno) != bp->b_bn) 10130f712c9SDave Chinner return false; 10230f712c9SDave Chinner } else { 10330f712c9SDave Chinner if (hdr->magic != cpu_to_be32(XFS_DIR2_FREE_MAGIC)) 10430f712c9SDave Chinner return false; 10530f712c9SDave Chinner } 10630f712c9SDave Chinner 10730f712c9SDave Chinner /* XXX: should bounds check the xfs_dir3_icfree_hdr here */ 10830f712c9SDave Chinner 10930f712c9SDave Chinner return true; 11030f712c9SDave Chinner } 11130f712c9SDave Chinner 11230f712c9SDave Chinner static void 11330f712c9SDave Chinner xfs_dir3_free_read_verify( 11430f712c9SDave Chinner struct xfs_buf *bp) 11530f712c9SDave Chinner { 11630f712c9SDave Chinner struct xfs_mount *mp = bp->b_target->bt_mount; 11730f712c9SDave Chinner 11830f712c9SDave Chinner if (xfs_sb_version_hascrc(&mp->m_sb) && 11930f712c9SDave Chinner !xfs_buf_verify_cksum(bp, XFS_DIR3_FREE_CRC_OFF)) 120*2451337dSDave Chinner xfs_buf_ioerror(bp, -EFSBADCRC); 12130f712c9SDave Chinner else if (!xfs_dir3_free_verify(bp)) 122*2451337dSDave Chinner xfs_buf_ioerror(bp, -EFSCORRUPTED); 12330f712c9SDave Chinner 12430f712c9SDave Chinner if (bp->b_error) 12530f712c9SDave Chinner xfs_verifier_error(bp); 12630f712c9SDave Chinner } 12730f712c9SDave Chinner 12830f712c9SDave Chinner static void 12930f712c9SDave Chinner xfs_dir3_free_write_verify( 13030f712c9SDave Chinner struct xfs_buf *bp) 13130f712c9SDave Chinner { 13230f712c9SDave Chinner struct xfs_mount *mp = bp->b_target->bt_mount; 13330f712c9SDave Chinner struct xfs_buf_log_item *bip = bp->b_fspriv; 13430f712c9SDave Chinner struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr; 13530f712c9SDave Chinner 13630f712c9SDave Chinner if (!xfs_dir3_free_verify(bp)) { 137*2451337dSDave Chinner xfs_buf_ioerror(bp, -EFSCORRUPTED); 13830f712c9SDave Chinner xfs_verifier_error(bp); 13930f712c9SDave Chinner return; 14030f712c9SDave Chinner } 14130f712c9SDave Chinner 14230f712c9SDave Chinner if (!xfs_sb_version_hascrc(&mp->m_sb)) 14330f712c9SDave Chinner return; 14430f712c9SDave Chinner 14530f712c9SDave Chinner if (bip) 14630f712c9SDave Chinner hdr3->lsn = cpu_to_be64(bip->bli_item.li_lsn); 14730f712c9SDave Chinner 14830f712c9SDave Chinner xfs_buf_update_cksum(bp, XFS_DIR3_FREE_CRC_OFF); 14930f712c9SDave Chinner } 15030f712c9SDave Chinner 15130f712c9SDave Chinner const struct xfs_buf_ops xfs_dir3_free_buf_ops = { 15230f712c9SDave Chinner .verify_read = xfs_dir3_free_read_verify, 15330f712c9SDave Chinner .verify_write = xfs_dir3_free_write_verify, 15430f712c9SDave Chinner }; 15530f712c9SDave Chinner 15630f712c9SDave Chinner 15730f712c9SDave Chinner static int 15830f712c9SDave Chinner __xfs_dir3_free_read( 15930f712c9SDave Chinner struct xfs_trans *tp, 16030f712c9SDave Chinner struct xfs_inode *dp, 16130f712c9SDave Chinner xfs_dablk_t fbno, 16230f712c9SDave Chinner xfs_daddr_t mappedbno, 16330f712c9SDave Chinner struct xfs_buf **bpp) 16430f712c9SDave Chinner { 16530f712c9SDave Chinner int err; 16630f712c9SDave Chinner 16730f712c9SDave Chinner err = xfs_da_read_buf(tp, dp, fbno, mappedbno, bpp, 16830f712c9SDave Chinner XFS_DATA_FORK, &xfs_dir3_free_buf_ops); 16930f712c9SDave Chinner 17030f712c9SDave Chinner /* try read returns without an error or *bpp if it lands in a hole */ 17130f712c9SDave Chinner if (!err && tp && *bpp) 17230f712c9SDave Chinner xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_FREE_BUF); 17330f712c9SDave Chinner return err; 17430f712c9SDave Chinner } 17530f712c9SDave Chinner 17630f712c9SDave Chinner int 17730f712c9SDave Chinner xfs_dir2_free_read( 17830f712c9SDave Chinner struct xfs_trans *tp, 17930f712c9SDave Chinner struct xfs_inode *dp, 18030f712c9SDave Chinner xfs_dablk_t fbno, 18130f712c9SDave Chinner struct xfs_buf **bpp) 18230f712c9SDave Chinner { 18330f712c9SDave Chinner return __xfs_dir3_free_read(tp, dp, fbno, -1, bpp); 18430f712c9SDave Chinner } 18530f712c9SDave Chinner 18630f712c9SDave Chinner static int 18730f712c9SDave Chinner xfs_dir2_free_try_read( 18830f712c9SDave Chinner struct xfs_trans *tp, 18930f712c9SDave Chinner struct xfs_inode *dp, 19030f712c9SDave Chinner xfs_dablk_t fbno, 19130f712c9SDave Chinner struct xfs_buf **bpp) 19230f712c9SDave Chinner { 19330f712c9SDave Chinner return __xfs_dir3_free_read(tp, dp, fbno, -2, bpp); 19430f712c9SDave Chinner } 19530f712c9SDave Chinner 19630f712c9SDave Chinner static int 19730f712c9SDave Chinner xfs_dir3_free_get_buf( 19830f712c9SDave Chinner xfs_da_args_t *args, 19930f712c9SDave Chinner xfs_dir2_db_t fbno, 20030f712c9SDave Chinner struct xfs_buf **bpp) 20130f712c9SDave Chinner { 20230f712c9SDave Chinner struct xfs_trans *tp = args->trans; 20330f712c9SDave Chinner struct xfs_inode *dp = args->dp; 20430f712c9SDave Chinner struct xfs_mount *mp = dp->i_mount; 20530f712c9SDave Chinner struct xfs_buf *bp; 20630f712c9SDave Chinner int error; 20730f712c9SDave Chinner struct xfs_dir3_icfree_hdr hdr; 20830f712c9SDave Chinner 20930f712c9SDave Chinner error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(args->geo, fbno), 21030f712c9SDave Chinner -1, &bp, XFS_DATA_FORK); 21130f712c9SDave Chinner if (error) 21230f712c9SDave Chinner return error; 21330f712c9SDave Chinner 21430f712c9SDave Chinner xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_FREE_BUF); 21530f712c9SDave Chinner bp->b_ops = &xfs_dir3_free_buf_ops; 21630f712c9SDave Chinner 21730f712c9SDave Chinner /* 21830f712c9SDave Chinner * Initialize the new block to be empty, and remember 21930f712c9SDave Chinner * its first slot as our empty slot. 22030f712c9SDave Chinner */ 22130f712c9SDave Chinner memset(bp->b_addr, 0, sizeof(struct xfs_dir3_free_hdr)); 22230f712c9SDave Chinner memset(&hdr, 0, sizeof(hdr)); 22330f712c9SDave Chinner 22430f712c9SDave Chinner if (xfs_sb_version_hascrc(&mp->m_sb)) { 22530f712c9SDave Chinner struct xfs_dir3_free_hdr *hdr3 = bp->b_addr; 22630f712c9SDave Chinner 22730f712c9SDave Chinner hdr.magic = XFS_DIR3_FREE_MAGIC; 22830f712c9SDave Chinner 22930f712c9SDave Chinner hdr3->hdr.blkno = cpu_to_be64(bp->b_bn); 23030f712c9SDave Chinner hdr3->hdr.owner = cpu_to_be64(dp->i_ino); 23130f712c9SDave Chinner uuid_copy(&hdr3->hdr.uuid, &mp->m_sb.sb_uuid); 23230f712c9SDave Chinner } else 23330f712c9SDave Chinner hdr.magic = XFS_DIR2_FREE_MAGIC; 23430f712c9SDave Chinner dp->d_ops->free_hdr_to_disk(bp->b_addr, &hdr); 23530f712c9SDave Chinner *bpp = bp; 23630f712c9SDave Chinner return 0; 23730f712c9SDave Chinner } 23830f712c9SDave Chinner 23930f712c9SDave Chinner /* 24030f712c9SDave Chinner * Log entries from a freespace block. 24130f712c9SDave Chinner */ 24230f712c9SDave Chinner STATIC void 24330f712c9SDave Chinner xfs_dir2_free_log_bests( 24430f712c9SDave Chinner struct xfs_da_args *args, 24530f712c9SDave Chinner struct xfs_buf *bp, 24630f712c9SDave Chinner int first, /* first entry to log */ 24730f712c9SDave Chinner int last) /* last entry to log */ 24830f712c9SDave Chinner { 24930f712c9SDave Chinner xfs_dir2_free_t *free; /* freespace structure */ 25030f712c9SDave Chinner __be16 *bests; 25130f712c9SDave Chinner 25230f712c9SDave Chinner free = bp->b_addr; 25330f712c9SDave Chinner bests = args->dp->d_ops->free_bests_p(free); 25430f712c9SDave Chinner ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) || 25530f712c9SDave Chinner free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC)); 25630f712c9SDave Chinner xfs_trans_log_buf(args->trans, bp, 25730f712c9SDave Chinner (uint)((char *)&bests[first] - (char *)free), 25830f712c9SDave Chinner (uint)((char *)&bests[last] - (char *)free + 25930f712c9SDave Chinner sizeof(bests[0]) - 1)); 26030f712c9SDave Chinner } 26130f712c9SDave Chinner 26230f712c9SDave Chinner /* 26330f712c9SDave Chinner * Log header from a freespace block. 26430f712c9SDave Chinner */ 26530f712c9SDave Chinner static void 26630f712c9SDave Chinner xfs_dir2_free_log_header( 26730f712c9SDave Chinner struct xfs_da_args *args, 26830f712c9SDave Chinner struct xfs_buf *bp) 26930f712c9SDave Chinner { 27030f712c9SDave Chinner #ifdef DEBUG 27130f712c9SDave Chinner xfs_dir2_free_t *free; /* freespace structure */ 27230f712c9SDave Chinner 27330f712c9SDave Chinner free = bp->b_addr; 27430f712c9SDave Chinner ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) || 27530f712c9SDave Chinner free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC)); 27630f712c9SDave Chinner #endif 27730f712c9SDave Chinner xfs_trans_log_buf(args->trans, bp, 0, 27830f712c9SDave Chinner args->dp->d_ops->free_hdr_size - 1); 27930f712c9SDave Chinner } 28030f712c9SDave Chinner 28130f712c9SDave Chinner /* 28230f712c9SDave Chinner * Convert a leaf-format directory to a node-format directory. 28330f712c9SDave Chinner * We need to change the magic number of the leaf block, and copy 28430f712c9SDave Chinner * the freespace table out of the leaf block into its own block. 28530f712c9SDave Chinner */ 28630f712c9SDave Chinner int /* error */ 28730f712c9SDave Chinner xfs_dir2_leaf_to_node( 28830f712c9SDave Chinner xfs_da_args_t *args, /* operation arguments */ 28930f712c9SDave Chinner struct xfs_buf *lbp) /* leaf buffer */ 29030f712c9SDave Chinner { 29130f712c9SDave Chinner xfs_inode_t *dp; /* incore directory inode */ 29230f712c9SDave Chinner int error; /* error return value */ 29330f712c9SDave Chinner struct xfs_buf *fbp; /* freespace buffer */ 29430f712c9SDave Chinner xfs_dir2_db_t fdb; /* freespace block number */ 29530f712c9SDave Chinner xfs_dir2_free_t *free; /* freespace structure */ 29630f712c9SDave Chinner __be16 *from; /* pointer to freespace entry */ 29730f712c9SDave Chinner int i; /* leaf freespace index */ 29830f712c9SDave Chinner xfs_dir2_leaf_t *leaf; /* leaf structure */ 29930f712c9SDave Chinner xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */ 30030f712c9SDave Chinner xfs_mount_t *mp; /* filesystem mount point */ 30130f712c9SDave Chinner int n; /* count of live freespc ents */ 30230f712c9SDave Chinner xfs_dir2_data_off_t off; /* freespace entry value */ 30330f712c9SDave Chinner __be16 *to; /* pointer to freespace entry */ 30430f712c9SDave Chinner xfs_trans_t *tp; /* transaction pointer */ 30530f712c9SDave Chinner struct xfs_dir3_icfree_hdr freehdr; 30630f712c9SDave Chinner 30730f712c9SDave Chinner trace_xfs_dir2_leaf_to_node(args); 30830f712c9SDave Chinner 30930f712c9SDave Chinner dp = args->dp; 31030f712c9SDave Chinner mp = dp->i_mount; 31130f712c9SDave Chinner tp = args->trans; 31230f712c9SDave Chinner /* 31330f712c9SDave Chinner * Add a freespace block to the directory. 31430f712c9SDave Chinner */ 31530f712c9SDave Chinner if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fdb))) { 31630f712c9SDave Chinner return error; 31730f712c9SDave Chinner } 31830f712c9SDave Chinner ASSERT(fdb == xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET)); 31930f712c9SDave Chinner /* 32030f712c9SDave Chinner * Get the buffer for the new freespace block. 32130f712c9SDave Chinner */ 32230f712c9SDave Chinner error = xfs_dir3_free_get_buf(args, fdb, &fbp); 32330f712c9SDave Chinner if (error) 32430f712c9SDave Chinner return error; 32530f712c9SDave Chinner 32630f712c9SDave Chinner free = fbp->b_addr; 32730f712c9SDave Chinner dp->d_ops->free_hdr_from_disk(&freehdr, free); 32830f712c9SDave Chinner leaf = lbp->b_addr; 32930f712c9SDave Chinner ltp = xfs_dir2_leaf_tail_p(args->geo, leaf); 33030f712c9SDave Chinner ASSERT(be32_to_cpu(ltp->bestcount) <= 33130f712c9SDave Chinner (uint)dp->i_d.di_size / args->geo->blksize); 33230f712c9SDave Chinner 33330f712c9SDave Chinner /* 33430f712c9SDave Chinner * Copy freespace entries from the leaf block to the new block. 33530f712c9SDave Chinner * Count active entries. 33630f712c9SDave Chinner */ 33730f712c9SDave Chinner from = xfs_dir2_leaf_bests_p(ltp); 33830f712c9SDave Chinner to = dp->d_ops->free_bests_p(free); 33930f712c9SDave Chinner for (i = n = 0; i < be32_to_cpu(ltp->bestcount); i++, from++, to++) { 34030f712c9SDave Chinner if ((off = be16_to_cpu(*from)) != NULLDATAOFF) 34130f712c9SDave Chinner n++; 34230f712c9SDave Chinner *to = cpu_to_be16(off); 34330f712c9SDave Chinner } 34430f712c9SDave Chinner 34530f712c9SDave Chinner /* 34630f712c9SDave Chinner * Now initialize the freespace block header. 34730f712c9SDave Chinner */ 34830f712c9SDave Chinner freehdr.nused = n; 34930f712c9SDave Chinner freehdr.nvalid = be32_to_cpu(ltp->bestcount); 35030f712c9SDave Chinner 35130f712c9SDave Chinner dp->d_ops->free_hdr_to_disk(fbp->b_addr, &freehdr); 35230f712c9SDave Chinner xfs_dir2_free_log_bests(args, fbp, 0, freehdr.nvalid - 1); 35330f712c9SDave Chinner xfs_dir2_free_log_header(args, fbp); 35430f712c9SDave Chinner 35530f712c9SDave Chinner /* 35630f712c9SDave Chinner * Converting the leaf to a leafnode is just a matter of changing the 35730f712c9SDave Chinner * magic number and the ops. Do the change directly to the buffer as 35830f712c9SDave Chinner * it's less work (and less code) than decoding the header to host 35930f712c9SDave Chinner * format and back again. 36030f712c9SDave Chinner */ 36130f712c9SDave Chinner if (leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC)) 36230f712c9SDave Chinner leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAFN_MAGIC); 36330f712c9SDave Chinner else 36430f712c9SDave Chinner leaf->hdr.info.magic = cpu_to_be16(XFS_DIR3_LEAFN_MAGIC); 36530f712c9SDave Chinner lbp->b_ops = &xfs_dir3_leafn_buf_ops; 36630f712c9SDave Chinner xfs_trans_buf_set_type(tp, lbp, XFS_BLFT_DIR_LEAFN_BUF); 36730f712c9SDave Chinner xfs_dir3_leaf_log_header(args, lbp); 36830f712c9SDave Chinner xfs_dir3_leaf_check(dp, lbp); 36930f712c9SDave Chinner return 0; 37030f712c9SDave Chinner } 37130f712c9SDave Chinner 37230f712c9SDave Chinner /* 37330f712c9SDave Chinner * Add a leaf entry to a leaf block in a node-form directory. 37430f712c9SDave Chinner * The other work necessary is done from the caller. 37530f712c9SDave Chinner */ 37630f712c9SDave Chinner static int /* error */ 37730f712c9SDave Chinner xfs_dir2_leafn_add( 37830f712c9SDave Chinner struct xfs_buf *bp, /* leaf buffer */ 37930f712c9SDave Chinner xfs_da_args_t *args, /* operation arguments */ 38030f712c9SDave Chinner int index) /* insertion pt for new entry */ 38130f712c9SDave Chinner { 38230f712c9SDave Chinner int compact; /* compacting stale leaves */ 38330f712c9SDave Chinner xfs_inode_t *dp; /* incore directory inode */ 38430f712c9SDave Chinner int highstale; /* next stale entry */ 38530f712c9SDave Chinner xfs_dir2_leaf_t *leaf; /* leaf structure */ 38630f712c9SDave Chinner xfs_dir2_leaf_entry_t *lep; /* leaf entry */ 38730f712c9SDave Chinner int lfloghigh; /* high leaf entry logging */ 38830f712c9SDave Chinner int lfloglow; /* low leaf entry logging */ 38930f712c9SDave Chinner int lowstale; /* previous stale entry */ 39030f712c9SDave Chinner xfs_mount_t *mp; /* filesystem mount point */ 39130f712c9SDave Chinner xfs_trans_t *tp; /* transaction pointer */ 39230f712c9SDave Chinner struct xfs_dir3_icleaf_hdr leafhdr; 39330f712c9SDave Chinner struct xfs_dir2_leaf_entry *ents; 39430f712c9SDave Chinner 39530f712c9SDave Chinner trace_xfs_dir2_leafn_add(args, index); 39630f712c9SDave Chinner 39730f712c9SDave Chinner dp = args->dp; 39830f712c9SDave Chinner mp = dp->i_mount; 39930f712c9SDave Chinner tp = args->trans; 40030f712c9SDave Chinner leaf = bp->b_addr; 40130f712c9SDave Chinner dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf); 40230f712c9SDave Chinner ents = dp->d_ops->leaf_ents_p(leaf); 40330f712c9SDave Chinner 40430f712c9SDave Chinner /* 40530f712c9SDave Chinner * Quick check just to make sure we are not going to index 40630f712c9SDave Chinner * into other peoples memory 40730f712c9SDave Chinner */ 40830f712c9SDave Chinner if (index < 0) 409*2451337dSDave Chinner return -EFSCORRUPTED; 41030f712c9SDave Chinner 41130f712c9SDave Chinner /* 41230f712c9SDave Chinner * If there are already the maximum number of leaf entries in 41330f712c9SDave Chinner * the block, if there are no stale entries it won't fit. 41430f712c9SDave Chinner * Caller will do a split. If there are stale entries we'll do 41530f712c9SDave Chinner * a compact. 41630f712c9SDave Chinner */ 41730f712c9SDave Chinner 41830f712c9SDave Chinner if (leafhdr.count == dp->d_ops->leaf_max_ents(args->geo)) { 41930f712c9SDave Chinner if (!leafhdr.stale) 420*2451337dSDave Chinner return -ENOSPC; 42130f712c9SDave Chinner compact = leafhdr.stale > 1; 42230f712c9SDave Chinner } else 42330f712c9SDave Chinner compact = 0; 42430f712c9SDave Chinner ASSERT(index == 0 || be32_to_cpu(ents[index - 1].hashval) <= args->hashval); 42530f712c9SDave Chinner ASSERT(index == leafhdr.count || 42630f712c9SDave Chinner be32_to_cpu(ents[index].hashval) >= args->hashval); 42730f712c9SDave Chinner 42830f712c9SDave Chinner if (args->op_flags & XFS_DA_OP_JUSTCHECK) 42930f712c9SDave Chinner return 0; 43030f712c9SDave Chinner 43130f712c9SDave Chinner /* 43230f712c9SDave Chinner * Compact out all but one stale leaf entry. Leaves behind 43330f712c9SDave Chinner * the entry closest to index. 43430f712c9SDave Chinner */ 43530f712c9SDave Chinner if (compact) 43630f712c9SDave Chinner xfs_dir3_leaf_compact_x1(&leafhdr, ents, &index, &lowstale, 43730f712c9SDave Chinner &highstale, &lfloglow, &lfloghigh); 43830f712c9SDave Chinner else if (leafhdr.stale) { 43930f712c9SDave Chinner /* 44030f712c9SDave Chinner * Set impossible logging indices for this case. 44130f712c9SDave Chinner */ 44230f712c9SDave Chinner lfloglow = leafhdr.count; 44330f712c9SDave Chinner lfloghigh = -1; 44430f712c9SDave Chinner } 44530f712c9SDave Chinner 44630f712c9SDave Chinner /* 44730f712c9SDave Chinner * Insert the new entry, log everything. 44830f712c9SDave Chinner */ 44930f712c9SDave Chinner lep = xfs_dir3_leaf_find_entry(&leafhdr, ents, index, compact, lowstale, 45030f712c9SDave Chinner highstale, &lfloglow, &lfloghigh); 45130f712c9SDave Chinner 45230f712c9SDave Chinner lep->hashval = cpu_to_be32(args->hashval); 45330f712c9SDave Chinner lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(args->geo, 45430f712c9SDave Chinner args->blkno, args->index)); 45530f712c9SDave Chinner 45630f712c9SDave Chinner dp->d_ops->leaf_hdr_to_disk(leaf, &leafhdr); 45730f712c9SDave Chinner xfs_dir3_leaf_log_header(args, bp); 45830f712c9SDave Chinner xfs_dir3_leaf_log_ents(args, bp, lfloglow, lfloghigh); 45930f712c9SDave Chinner xfs_dir3_leaf_check(dp, bp); 46030f712c9SDave Chinner return 0; 46130f712c9SDave Chinner } 46230f712c9SDave Chinner 46330f712c9SDave Chinner #ifdef DEBUG 46430f712c9SDave Chinner static void 46530f712c9SDave Chinner xfs_dir2_free_hdr_check( 46630f712c9SDave Chinner struct xfs_inode *dp, 46730f712c9SDave Chinner struct xfs_buf *bp, 46830f712c9SDave Chinner xfs_dir2_db_t db) 46930f712c9SDave Chinner { 47030f712c9SDave Chinner struct xfs_dir3_icfree_hdr hdr; 47130f712c9SDave Chinner 47230f712c9SDave Chinner dp->d_ops->free_hdr_from_disk(&hdr, bp->b_addr); 47330f712c9SDave Chinner 47430f712c9SDave Chinner ASSERT((hdr.firstdb % 47530f712c9SDave Chinner dp->d_ops->free_max_bests(dp->i_mount->m_dir_geo)) == 0); 47630f712c9SDave Chinner ASSERT(hdr.firstdb <= db); 47730f712c9SDave Chinner ASSERT(db < hdr.firstdb + hdr.nvalid); 47830f712c9SDave Chinner } 47930f712c9SDave Chinner #else 48030f712c9SDave Chinner #define xfs_dir2_free_hdr_check(dp, bp, db) 48130f712c9SDave Chinner #endif /* DEBUG */ 48230f712c9SDave Chinner 48330f712c9SDave Chinner /* 48430f712c9SDave Chinner * Return the last hash value in the leaf. 48530f712c9SDave Chinner * Stale entries are ok. 48630f712c9SDave Chinner */ 48730f712c9SDave Chinner xfs_dahash_t /* hash value */ 48830f712c9SDave Chinner xfs_dir2_leafn_lasthash( 48930f712c9SDave Chinner struct xfs_inode *dp, 49030f712c9SDave Chinner struct xfs_buf *bp, /* leaf buffer */ 49130f712c9SDave Chinner int *count) /* count of entries in leaf */ 49230f712c9SDave Chinner { 49330f712c9SDave Chinner struct xfs_dir2_leaf *leaf = bp->b_addr; 49430f712c9SDave Chinner struct xfs_dir2_leaf_entry *ents; 49530f712c9SDave Chinner struct xfs_dir3_icleaf_hdr leafhdr; 49630f712c9SDave Chinner 49730f712c9SDave Chinner dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf); 49830f712c9SDave Chinner 49930f712c9SDave Chinner ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC || 50030f712c9SDave Chinner leafhdr.magic == XFS_DIR3_LEAFN_MAGIC); 50130f712c9SDave Chinner 50230f712c9SDave Chinner if (count) 50330f712c9SDave Chinner *count = leafhdr.count; 50430f712c9SDave Chinner if (!leafhdr.count) 50530f712c9SDave Chinner return 0; 50630f712c9SDave Chinner 50730f712c9SDave Chinner ents = dp->d_ops->leaf_ents_p(leaf); 50830f712c9SDave Chinner return be32_to_cpu(ents[leafhdr.count - 1].hashval); 50930f712c9SDave Chinner } 51030f712c9SDave Chinner 51130f712c9SDave Chinner /* 51230f712c9SDave Chinner * Look up a leaf entry for space to add a name in a node-format leaf block. 51330f712c9SDave Chinner * The extrablk in state is a freespace block. 51430f712c9SDave Chinner */ 51530f712c9SDave Chinner STATIC int 51630f712c9SDave Chinner xfs_dir2_leafn_lookup_for_addname( 51730f712c9SDave Chinner struct xfs_buf *bp, /* leaf buffer */ 51830f712c9SDave Chinner xfs_da_args_t *args, /* operation arguments */ 51930f712c9SDave Chinner int *indexp, /* out: leaf entry index */ 52030f712c9SDave Chinner xfs_da_state_t *state) /* state to fill in */ 52130f712c9SDave Chinner { 52230f712c9SDave Chinner struct xfs_buf *curbp = NULL; /* current data/free buffer */ 52330f712c9SDave Chinner xfs_dir2_db_t curdb = -1; /* current data block number */ 52430f712c9SDave Chinner xfs_dir2_db_t curfdb = -1; /* current free block number */ 52530f712c9SDave Chinner xfs_inode_t *dp; /* incore directory inode */ 52630f712c9SDave Chinner int error; /* error return value */ 52730f712c9SDave Chinner int fi; /* free entry index */ 52830f712c9SDave Chinner xfs_dir2_free_t *free = NULL; /* free block structure */ 52930f712c9SDave Chinner int index; /* leaf entry index */ 53030f712c9SDave Chinner xfs_dir2_leaf_t *leaf; /* leaf structure */ 53130f712c9SDave Chinner int length; /* length of new data entry */ 53230f712c9SDave Chinner xfs_dir2_leaf_entry_t *lep; /* leaf entry */ 53330f712c9SDave Chinner xfs_mount_t *mp; /* filesystem mount point */ 53430f712c9SDave Chinner xfs_dir2_db_t newdb; /* new data block number */ 53530f712c9SDave Chinner xfs_dir2_db_t newfdb; /* new free block number */ 53630f712c9SDave Chinner xfs_trans_t *tp; /* transaction pointer */ 53730f712c9SDave Chinner struct xfs_dir2_leaf_entry *ents; 53830f712c9SDave Chinner struct xfs_dir3_icleaf_hdr leafhdr; 53930f712c9SDave Chinner 54030f712c9SDave Chinner dp = args->dp; 54130f712c9SDave Chinner tp = args->trans; 54230f712c9SDave Chinner mp = dp->i_mount; 54330f712c9SDave Chinner leaf = bp->b_addr; 54430f712c9SDave Chinner dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf); 54530f712c9SDave Chinner ents = dp->d_ops->leaf_ents_p(leaf); 54630f712c9SDave Chinner 54730f712c9SDave Chinner xfs_dir3_leaf_check(dp, bp); 54830f712c9SDave Chinner ASSERT(leafhdr.count > 0); 54930f712c9SDave Chinner 55030f712c9SDave Chinner /* 55130f712c9SDave Chinner * Look up the hash value in the leaf entries. 55230f712c9SDave Chinner */ 55330f712c9SDave Chinner index = xfs_dir2_leaf_search_hash(args, bp); 55430f712c9SDave Chinner /* 55530f712c9SDave Chinner * Do we have a buffer coming in? 55630f712c9SDave Chinner */ 55730f712c9SDave Chinner if (state->extravalid) { 55830f712c9SDave Chinner /* If so, it's a free block buffer, get the block number. */ 55930f712c9SDave Chinner curbp = state->extrablk.bp; 56030f712c9SDave Chinner curfdb = state->extrablk.blkno; 56130f712c9SDave Chinner free = curbp->b_addr; 56230f712c9SDave Chinner ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) || 56330f712c9SDave Chinner free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC)); 56430f712c9SDave Chinner } 56530f712c9SDave Chinner length = dp->d_ops->data_entsize(args->namelen); 56630f712c9SDave Chinner /* 56730f712c9SDave Chinner * Loop over leaf entries with the right hash value. 56830f712c9SDave Chinner */ 56930f712c9SDave Chinner for (lep = &ents[index]; 57030f712c9SDave Chinner index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval; 57130f712c9SDave Chinner lep++, index++) { 57230f712c9SDave Chinner /* 57330f712c9SDave Chinner * Skip stale leaf entries. 57430f712c9SDave Chinner */ 57530f712c9SDave Chinner if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR) 57630f712c9SDave Chinner continue; 57730f712c9SDave Chinner /* 57830f712c9SDave Chinner * Pull the data block number from the entry. 57930f712c9SDave Chinner */ 58030f712c9SDave Chinner newdb = xfs_dir2_dataptr_to_db(args->geo, 58130f712c9SDave Chinner be32_to_cpu(lep->address)); 58230f712c9SDave Chinner /* 58330f712c9SDave Chinner * For addname, we're looking for a place to put the new entry. 58430f712c9SDave Chinner * We want to use a data block with an entry of equal 58530f712c9SDave Chinner * hash value to ours if there is one with room. 58630f712c9SDave Chinner * 58730f712c9SDave Chinner * If this block isn't the data block we already have 58830f712c9SDave Chinner * in hand, take a look at it. 58930f712c9SDave Chinner */ 59030f712c9SDave Chinner if (newdb != curdb) { 59130f712c9SDave Chinner __be16 *bests; 59230f712c9SDave Chinner 59330f712c9SDave Chinner curdb = newdb; 59430f712c9SDave Chinner /* 59530f712c9SDave Chinner * Convert the data block to the free block 59630f712c9SDave Chinner * holding its freespace information. 59730f712c9SDave Chinner */ 59830f712c9SDave Chinner newfdb = dp->d_ops->db_to_fdb(args->geo, newdb); 59930f712c9SDave Chinner /* 60030f712c9SDave Chinner * If it's not the one we have in hand, read it in. 60130f712c9SDave Chinner */ 60230f712c9SDave Chinner if (newfdb != curfdb) { 60330f712c9SDave Chinner /* 60430f712c9SDave Chinner * If we had one before, drop it. 60530f712c9SDave Chinner */ 60630f712c9SDave Chinner if (curbp) 60730f712c9SDave Chinner xfs_trans_brelse(tp, curbp); 60830f712c9SDave Chinner 60930f712c9SDave Chinner error = xfs_dir2_free_read(tp, dp, 61030f712c9SDave Chinner xfs_dir2_db_to_da(args->geo, 61130f712c9SDave Chinner newfdb), 61230f712c9SDave Chinner &curbp); 61330f712c9SDave Chinner if (error) 61430f712c9SDave Chinner return error; 61530f712c9SDave Chinner free = curbp->b_addr; 61630f712c9SDave Chinner 61730f712c9SDave Chinner xfs_dir2_free_hdr_check(dp, curbp, curdb); 61830f712c9SDave Chinner } 61930f712c9SDave Chinner /* 62030f712c9SDave Chinner * Get the index for our entry. 62130f712c9SDave Chinner */ 62230f712c9SDave Chinner fi = dp->d_ops->db_to_fdindex(args->geo, curdb); 62330f712c9SDave Chinner /* 62430f712c9SDave Chinner * If it has room, return it. 62530f712c9SDave Chinner */ 62630f712c9SDave Chinner bests = dp->d_ops->free_bests_p(free); 62730f712c9SDave Chinner if (unlikely(bests[fi] == cpu_to_be16(NULLDATAOFF))) { 62830f712c9SDave Chinner XFS_ERROR_REPORT("xfs_dir2_leafn_lookup_int", 62930f712c9SDave Chinner XFS_ERRLEVEL_LOW, mp); 63030f712c9SDave Chinner if (curfdb != newfdb) 63130f712c9SDave Chinner xfs_trans_brelse(tp, curbp); 632*2451337dSDave Chinner return -EFSCORRUPTED; 63330f712c9SDave Chinner } 63430f712c9SDave Chinner curfdb = newfdb; 63530f712c9SDave Chinner if (be16_to_cpu(bests[fi]) >= length) 63630f712c9SDave Chinner goto out; 63730f712c9SDave Chinner } 63830f712c9SDave Chinner } 63930f712c9SDave Chinner /* Didn't find any space */ 64030f712c9SDave Chinner fi = -1; 64130f712c9SDave Chinner out: 64230f712c9SDave Chinner ASSERT(args->op_flags & XFS_DA_OP_OKNOENT); 64330f712c9SDave Chinner if (curbp) { 64430f712c9SDave Chinner /* Giving back a free block. */ 64530f712c9SDave Chinner state->extravalid = 1; 64630f712c9SDave Chinner state->extrablk.bp = curbp; 64730f712c9SDave Chinner state->extrablk.index = fi; 64830f712c9SDave Chinner state->extrablk.blkno = curfdb; 64930f712c9SDave Chinner 65030f712c9SDave Chinner /* 65130f712c9SDave Chinner * Important: this magic number is not in the buffer - it's for 65230f712c9SDave Chinner * buffer type information and therefore only the free/data type 65330f712c9SDave Chinner * matters here, not whether CRCs are enabled or not. 65430f712c9SDave Chinner */ 65530f712c9SDave Chinner state->extrablk.magic = XFS_DIR2_FREE_MAGIC; 65630f712c9SDave Chinner } else { 65730f712c9SDave Chinner state->extravalid = 0; 65830f712c9SDave Chinner } 65930f712c9SDave Chinner /* 66030f712c9SDave Chinner * Return the index, that will be the insertion point. 66130f712c9SDave Chinner */ 66230f712c9SDave Chinner *indexp = index; 663*2451337dSDave Chinner return -ENOENT; 66430f712c9SDave Chinner } 66530f712c9SDave Chinner 66630f712c9SDave Chinner /* 66730f712c9SDave Chinner * Look up a leaf entry in a node-format leaf block. 66830f712c9SDave Chinner * The extrablk in state a data block. 66930f712c9SDave Chinner */ 67030f712c9SDave Chinner STATIC int 67130f712c9SDave Chinner xfs_dir2_leafn_lookup_for_entry( 67230f712c9SDave Chinner struct xfs_buf *bp, /* leaf buffer */ 67330f712c9SDave Chinner xfs_da_args_t *args, /* operation arguments */ 67430f712c9SDave Chinner int *indexp, /* out: leaf entry index */ 67530f712c9SDave Chinner xfs_da_state_t *state) /* state to fill in */ 67630f712c9SDave Chinner { 67730f712c9SDave Chinner struct xfs_buf *curbp = NULL; /* current data/free buffer */ 67830f712c9SDave Chinner xfs_dir2_db_t curdb = -1; /* current data block number */ 67930f712c9SDave Chinner xfs_dir2_data_entry_t *dep; /* data block entry */ 68030f712c9SDave Chinner xfs_inode_t *dp; /* incore directory inode */ 68130f712c9SDave Chinner int error; /* error return value */ 68230f712c9SDave Chinner int index; /* leaf entry index */ 68330f712c9SDave Chinner xfs_dir2_leaf_t *leaf; /* leaf structure */ 68430f712c9SDave Chinner xfs_dir2_leaf_entry_t *lep; /* leaf entry */ 68530f712c9SDave Chinner xfs_mount_t *mp; /* filesystem mount point */ 68630f712c9SDave Chinner xfs_dir2_db_t newdb; /* new data block number */ 68730f712c9SDave Chinner xfs_trans_t *tp; /* transaction pointer */ 68830f712c9SDave Chinner enum xfs_dacmp cmp; /* comparison result */ 68930f712c9SDave Chinner struct xfs_dir2_leaf_entry *ents; 69030f712c9SDave Chinner struct xfs_dir3_icleaf_hdr leafhdr; 69130f712c9SDave Chinner 69230f712c9SDave Chinner dp = args->dp; 69330f712c9SDave Chinner tp = args->trans; 69430f712c9SDave Chinner mp = dp->i_mount; 69530f712c9SDave Chinner leaf = bp->b_addr; 69630f712c9SDave Chinner dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf); 69730f712c9SDave Chinner ents = dp->d_ops->leaf_ents_p(leaf); 69830f712c9SDave Chinner 69930f712c9SDave Chinner xfs_dir3_leaf_check(dp, bp); 70030f712c9SDave Chinner ASSERT(leafhdr.count > 0); 70130f712c9SDave Chinner 70230f712c9SDave Chinner /* 70330f712c9SDave Chinner * Look up the hash value in the leaf entries. 70430f712c9SDave Chinner */ 70530f712c9SDave Chinner index = xfs_dir2_leaf_search_hash(args, bp); 70630f712c9SDave Chinner /* 70730f712c9SDave Chinner * Do we have a buffer coming in? 70830f712c9SDave Chinner */ 70930f712c9SDave Chinner if (state->extravalid) { 71030f712c9SDave Chinner curbp = state->extrablk.bp; 71130f712c9SDave Chinner curdb = state->extrablk.blkno; 71230f712c9SDave Chinner } 71330f712c9SDave Chinner /* 71430f712c9SDave Chinner * Loop over leaf entries with the right hash value. 71530f712c9SDave Chinner */ 71630f712c9SDave Chinner for (lep = &ents[index]; 71730f712c9SDave Chinner index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval; 71830f712c9SDave Chinner lep++, index++) { 71930f712c9SDave Chinner /* 72030f712c9SDave Chinner * Skip stale leaf entries. 72130f712c9SDave Chinner */ 72230f712c9SDave Chinner if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR) 72330f712c9SDave Chinner continue; 72430f712c9SDave Chinner /* 72530f712c9SDave Chinner * Pull the data block number from the entry. 72630f712c9SDave Chinner */ 72730f712c9SDave Chinner newdb = xfs_dir2_dataptr_to_db(args->geo, 72830f712c9SDave Chinner be32_to_cpu(lep->address)); 72930f712c9SDave Chinner /* 73030f712c9SDave Chinner * Not adding a new entry, so we really want to find 73130f712c9SDave Chinner * the name given to us. 73230f712c9SDave Chinner * 73330f712c9SDave Chinner * If it's a different data block, go get it. 73430f712c9SDave Chinner */ 73530f712c9SDave Chinner if (newdb != curdb) { 73630f712c9SDave Chinner /* 73730f712c9SDave Chinner * If we had a block before that we aren't saving 73830f712c9SDave Chinner * for a CI name, drop it 73930f712c9SDave Chinner */ 74030f712c9SDave Chinner if (curbp && (args->cmpresult == XFS_CMP_DIFFERENT || 74130f712c9SDave Chinner curdb != state->extrablk.blkno)) 74230f712c9SDave Chinner xfs_trans_brelse(tp, curbp); 74330f712c9SDave Chinner /* 74430f712c9SDave Chinner * If needing the block that is saved with a CI match, 74530f712c9SDave Chinner * use it otherwise read in the new data block. 74630f712c9SDave Chinner */ 74730f712c9SDave Chinner if (args->cmpresult != XFS_CMP_DIFFERENT && 74830f712c9SDave Chinner newdb == state->extrablk.blkno) { 74930f712c9SDave Chinner ASSERT(state->extravalid); 75030f712c9SDave Chinner curbp = state->extrablk.bp; 75130f712c9SDave Chinner } else { 75230f712c9SDave Chinner error = xfs_dir3_data_read(tp, dp, 75330f712c9SDave Chinner xfs_dir2_db_to_da(args->geo, 75430f712c9SDave Chinner newdb), 75530f712c9SDave Chinner -1, &curbp); 75630f712c9SDave Chinner if (error) 75730f712c9SDave Chinner return error; 75830f712c9SDave Chinner } 75930f712c9SDave Chinner xfs_dir3_data_check(dp, curbp); 76030f712c9SDave Chinner curdb = newdb; 76130f712c9SDave Chinner } 76230f712c9SDave Chinner /* 76330f712c9SDave Chinner * Point to the data entry. 76430f712c9SDave Chinner */ 76530f712c9SDave Chinner dep = (xfs_dir2_data_entry_t *)((char *)curbp->b_addr + 76630f712c9SDave Chinner xfs_dir2_dataptr_to_off(args->geo, 76730f712c9SDave Chinner be32_to_cpu(lep->address))); 76830f712c9SDave Chinner /* 76930f712c9SDave Chinner * Compare the entry and if it's an exact match, return 77030f712c9SDave Chinner * EEXIST immediately. If it's the first case-insensitive 77130f712c9SDave Chinner * match, store the block & inode number and continue looking. 77230f712c9SDave Chinner */ 77330f712c9SDave Chinner cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen); 77430f712c9SDave Chinner if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) { 77530f712c9SDave Chinner /* If there is a CI match block, drop it */ 77630f712c9SDave Chinner if (args->cmpresult != XFS_CMP_DIFFERENT && 77730f712c9SDave Chinner curdb != state->extrablk.blkno) 77830f712c9SDave Chinner xfs_trans_brelse(tp, state->extrablk.bp); 77930f712c9SDave Chinner args->cmpresult = cmp; 78030f712c9SDave Chinner args->inumber = be64_to_cpu(dep->inumber); 78130f712c9SDave Chinner args->filetype = dp->d_ops->data_get_ftype(dep); 78230f712c9SDave Chinner *indexp = index; 78330f712c9SDave Chinner state->extravalid = 1; 78430f712c9SDave Chinner state->extrablk.bp = curbp; 78530f712c9SDave Chinner state->extrablk.blkno = curdb; 78630f712c9SDave Chinner state->extrablk.index = (int)((char *)dep - 78730f712c9SDave Chinner (char *)curbp->b_addr); 78830f712c9SDave Chinner state->extrablk.magic = XFS_DIR2_DATA_MAGIC; 78930f712c9SDave Chinner curbp->b_ops = &xfs_dir3_data_buf_ops; 79030f712c9SDave Chinner xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF); 79130f712c9SDave Chinner if (cmp == XFS_CMP_EXACT) 792*2451337dSDave Chinner return -EEXIST; 79330f712c9SDave Chinner } 79430f712c9SDave Chinner } 79530f712c9SDave Chinner ASSERT(index == leafhdr.count || (args->op_flags & XFS_DA_OP_OKNOENT)); 79630f712c9SDave Chinner if (curbp) { 79730f712c9SDave Chinner if (args->cmpresult == XFS_CMP_DIFFERENT) { 79830f712c9SDave Chinner /* Giving back last used data block. */ 79930f712c9SDave Chinner state->extravalid = 1; 80030f712c9SDave Chinner state->extrablk.bp = curbp; 80130f712c9SDave Chinner state->extrablk.index = -1; 80230f712c9SDave Chinner state->extrablk.blkno = curdb; 80330f712c9SDave Chinner state->extrablk.magic = XFS_DIR2_DATA_MAGIC; 80430f712c9SDave Chinner curbp->b_ops = &xfs_dir3_data_buf_ops; 80530f712c9SDave Chinner xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF); 80630f712c9SDave Chinner } else { 80730f712c9SDave Chinner /* If the curbp is not the CI match block, drop it */ 80830f712c9SDave Chinner if (state->extrablk.bp != curbp) 80930f712c9SDave Chinner xfs_trans_brelse(tp, curbp); 81030f712c9SDave Chinner } 81130f712c9SDave Chinner } else { 81230f712c9SDave Chinner state->extravalid = 0; 81330f712c9SDave Chinner } 81430f712c9SDave Chinner *indexp = index; 815*2451337dSDave Chinner return -ENOENT; 81630f712c9SDave Chinner } 81730f712c9SDave Chinner 81830f712c9SDave Chinner /* 81930f712c9SDave Chinner * Look up a leaf entry in a node-format leaf block. 82030f712c9SDave Chinner * If this is an addname then the extrablk in state is a freespace block, 82130f712c9SDave Chinner * otherwise it's a data block. 82230f712c9SDave Chinner */ 82330f712c9SDave Chinner int 82430f712c9SDave Chinner xfs_dir2_leafn_lookup_int( 82530f712c9SDave Chinner struct xfs_buf *bp, /* leaf buffer */ 82630f712c9SDave Chinner xfs_da_args_t *args, /* operation arguments */ 82730f712c9SDave Chinner int *indexp, /* out: leaf entry index */ 82830f712c9SDave Chinner xfs_da_state_t *state) /* state to fill in */ 82930f712c9SDave Chinner { 83030f712c9SDave Chinner if (args->op_flags & XFS_DA_OP_ADDNAME) 83130f712c9SDave Chinner return xfs_dir2_leafn_lookup_for_addname(bp, args, indexp, 83230f712c9SDave Chinner state); 83330f712c9SDave Chinner return xfs_dir2_leafn_lookup_for_entry(bp, args, indexp, state); 83430f712c9SDave Chinner } 83530f712c9SDave Chinner 83630f712c9SDave Chinner /* 83730f712c9SDave Chinner * Move count leaf entries from source to destination leaf. 83830f712c9SDave Chinner * Log entries and headers. Stale entries are preserved. 83930f712c9SDave Chinner */ 84030f712c9SDave Chinner static void 84130f712c9SDave Chinner xfs_dir3_leafn_moveents( 84230f712c9SDave Chinner xfs_da_args_t *args, /* operation arguments */ 84330f712c9SDave Chinner struct xfs_buf *bp_s, /* source */ 84430f712c9SDave Chinner struct xfs_dir3_icleaf_hdr *shdr, 84530f712c9SDave Chinner struct xfs_dir2_leaf_entry *sents, 84630f712c9SDave Chinner int start_s,/* source leaf index */ 84730f712c9SDave Chinner struct xfs_buf *bp_d, /* destination */ 84830f712c9SDave Chinner struct xfs_dir3_icleaf_hdr *dhdr, 84930f712c9SDave Chinner struct xfs_dir2_leaf_entry *dents, 85030f712c9SDave Chinner int start_d,/* destination leaf index */ 85130f712c9SDave Chinner int count) /* count of leaves to copy */ 85230f712c9SDave Chinner { 85330f712c9SDave Chinner int stale; /* count stale leaves copied */ 85430f712c9SDave Chinner 85530f712c9SDave Chinner trace_xfs_dir2_leafn_moveents(args, start_s, start_d, count); 85630f712c9SDave Chinner 85730f712c9SDave Chinner /* 85830f712c9SDave Chinner * Silently return if nothing to do. 85930f712c9SDave Chinner */ 86030f712c9SDave Chinner if (count == 0) 86130f712c9SDave Chinner return; 86230f712c9SDave Chinner 86330f712c9SDave Chinner /* 86430f712c9SDave Chinner * If the destination index is not the end of the current 86530f712c9SDave Chinner * destination leaf entries, open up a hole in the destination 86630f712c9SDave Chinner * to hold the new entries. 86730f712c9SDave Chinner */ 86830f712c9SDave Chinner if (start_d < dhdr->count) { 86930f712c9SDave Chinner memmove(&dents[start_d + count], &dents[start_d], 87030f712c9SDave Chinner (dhdr->count - start_d) * sizeof(xfs_dir2_leaf_entry_t)); 87130f712c9SDave Chinner xfs_dir3_leaf_log_ents(args, bp_d, start_d + count, 87230f712c9SDave Chinner count + dhdr->count - 1); 87330f712c9SDave Chinner } 87430f712c9SDave Chinner /* 87530f712c9SDave Chinner * If the source has stale leaves, count the ones in the copy range 87630f712c9SDave Chinner * so we can update the header correctly. 87730f712c9SDave Chinner */ 87830f712c9SDave Chinner if (shdr->stale) { 87930f712c9SDave Chinner int i; /* temp leaf index */ 88030f712c9SDave Chinner 88130f712c9SDave Chinner for (i = start_s, stale = 0; i < start_s + count; i++) { 88230f712c9SDave Chinner if (sents[i].address == 88330f712c9SDave Chinner cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) 88430f712c9SDave Chinner stale++; 88530f712c9SDave Chinner } 88630f712c9SDave Chinner } else 88730f712c9SDave Chinner stale = 0; 88830f712c9SDave Chinner /* 88930f712c9SDave Chinner * Copy the leaf entries from source to destination. 89030f712c9SDave Chinner */ 89130f712c9SDave Chinner memcpy(&dents[start_d], &sents[start_s], 89230f712c9SDave Chinner count * sizeof(xfs_dir2_leaf_entry_t)); 89330f712c9SDave Chinner xfs_dir3_leaf_log_ents(args, bp_d, start_d, start_d + count - 1); 89430f712c9SDave Chinner 89530f712c9SDave Chinner /* 89630f712c9SDave Chinner * If there are source entries after the ones we copied, 89730f712c9SDave Chinner * delete the ones we copied by sliding the next ones down. 89830f712c9SDave Chinner */ 89930f712c9SDave Chinner if (start_s + count < shdr->count) { 90030f712c9SDave Chinner memmove(&sents[start_s], &sents[start_s + count], 90130f712c9SDave Chinner count * sizeof(xfs_dir2_leaf_entry_t)); 90230f712c9SDave Chinner xfs_dir3_leaf_log_ents(args, bp_s, start_s, start_s + count - 1); 90330f712c9SDave Chinner } 90430f712c9SDave Chinner 90530f712c9SDave Chinner /* 90630f712c9SDave Chinner * Update the headers and log them. 90730f712c9SDave Chinner */ 90830f712c9SDave Chinner shdr->count -= count; 90930f712c9SDave Chinner shdr->stale -= stale; 91030f712c9SDave Chinner dhdr->count += count; 91130f712c9SDave Chinner dhdr->stale += stale; 91230f712c9SDave Chinner } 91330f712c9SDave Chinner 91430f712c9SDave Chinner /* 91530f712c9SDave Chinner * Determine the sort order of two leaf blocks. 91630f712c9SDave Chinner * Returns 1 if both are valid and leaf2 should be before leaf1, else 0. 91730f712c9SDave Chinner */ 91830f712c9SDave Chinner int /* sort order */ 91930f712c9SDave Chinner xfs_dir2_leafn_order( 92030f712c9SDave Chinner struct xfs_inode *dp, 92130f712c9SDave Chinner struct xfs_buf *leaf1_bp, /* leaf1 buffer */ 92230f712c9SDave Chinner struct xfs_buf *leaf2_bp) /* leaf2 buffer */ 92330f712c9SDave Chinner { 92430f712c9SDave Chinner struct xfs_dir2_leaf *leaf1 = leaf1_bp->b_addr; 92530f712c9SDave Chinner struct xfs_dir2_leaf *leaf2 = leaf2_bp->b_addr; 92630f712c9SDave Chinner struct xfs_dir2_leaf_entry *ents1; 92730f712c9SDave Chinner struct xfs_dir2_leaf_entry *ents2; 92830f712c9SDave Chinner struct xfs_dir3_icleaf_hdr hdr1; 92930f712c9SDave Chinner struct xfs_dir3_icleaf_hdr hdr2; 93030f712c9SDave Chinner 93130f712c9SDave Chinner dp->d_ops->leaf_hdr_from_disk(&hdr1, leaf1); 93230f712c9SDave Chinner dp->d_ops->leaf_hdr_from_disk(&hdr2, leaf2); 93330f712c9SDave Chinner ents1 = dp->d_ops->leaf_ents_p(leaf1); 93430f712c9SDave Chinner ents2 = dp->d_ops->leaf_ents_p(leaf2); 93530f712c9SDave Chinner 93630f712c9SDave Chinner if (hdr1.count > 0 && hdr2.count > 0 && 93730f712c9SDave Chinner (be32_to_cpu(ents2[0].hashval) < be32_to_cpu(ents1[0].hashval) || 93830f712c9SDave Chinner be32_to_cpu(ents2[hdr2.count - 1].hashval) < 93930f712c9SDave Chinner be32_to_cpu(ents1[hdr1.count - 1].hashval))) 94030f712c9SDave Chinner return 1; 94130f712c9SDave Chinner return 0; 94230f712c9SDave Chinner } 94330f712c9SDave Chinner 94430f712c9SDave Chinner /* 94530f712c9SDave Chinner * Rebalance leaf entries between two leaf blocks. 94630f712c9SDave Chinner * This is actually only called when the second block is new, 94730f712c9SDave Chinner * though the code deals with the general case. 94830f712c9SDave Chinner * A new entry will be inserted in one of the blocks, and that 94930f712c9SDave Chinner * entry is taken into account when balancing. 95030f712c9SDave Chinner */ 95130f712c9SDave Chinner static void 95230f712c9SDave Chinner xfs_dir2_leafn_rebalance( 95330f712c9SDave Chinner xfs_da_state_t *state, /* btree cursor */ 95430f712c9SDave Chinner xfs_da_state_blk_t *blk1, /* first btree block */ 95530f712c9SDave Chinner xfs_da_state_blk_t *blk2) /* second btree block */ 95630f712c9SDave Chinner { 95730f712c9SDave Chinner xfs_da_args_t *args; /* operation arguments */ 95830f712c9SDave Chinner int count; /* count (& direction) leaves */ 95930f712c9SDave Chinner int isleft; /* new goes in left leaf */ 96030f712c9SDave Chinner xfs_dir2_leaf_t *leaf1; /* first leaf structure */ 96130f712c9SDave Chinner xfs_dir2_leaf_t *leaf2; /* second leaf structure */ 96230f712c9SDave Chinner int mid; /* midpoint leaf index */ 96330f712c9SDave Chinner #if defined(DEBUG) || defined(XFS_WARN) 96430f712c9SDave Chinner int oldstale; /* old count of stale leaves */ 96530f712c9SDave Chinner #endif 96630f712c9SDave Chinner int oldsum; /* old total leaf count */ 96730f712c9SDave Chinner int swap; /* swapped leaf blocks */ 96830f712c9SDave Chinner struct xfs_dir2_leaf_entry *ents1; 96930f712c9SDave Chinner struct xfs_dir2_leaf_entry *ents2; 97030f712c9SDave Chinner struct xfs_dir3_icleaf_hdr hdr1; 97130f712c9SDave Chinner struct xfs_dir3_icleaf_hdr hdr2; 97230f712c9SDave Chinner struct xfs_inode *dp = state->args->dp; 97330f712c9SDave Chinner 97430f712c9SDave Chinner args = state->args; 97530f712c9SDave Chinner /* 97630f712c9SDave Chinner * If the block order is wrong, swap the arguments. 97730f712c9SDave Chinner */ 97830f712c9SDave Chinner if ((swap = xfs_dir2_leafn_order(dp, blk1->bp, blk2->bp))) { 97930f712c9SDave Chinner xfs_da_state_blk_t *tmp; /* temp for block swap */ 98030f712c9SDave Chinner 98130f712c9SDave Chinner tmp = blk1; 98230f712c9SDave Chinner blk1 = blk2; 98330f712c9SDave Chinner blk2 = tmp; 98430f712c9SDave Chinner } 98530f712c9SDave Chinner leaf1 = blk1->bp->b_addr; 98630f712c9SDave Chinner leaf2 = blk2->bp->b_addr; 98730f712c9SDave Chinner dp->d_ops->leaf_hdr_from_disk(&hdr1, leaf1); 98830f712c9SDave Chinner dp->d_ops->leaf_hdr_from_disk(&hdr2, leaf2); 98930f712c9SDave Chinner ents1 = dp->d_ops->leaf_ents_p(leaf1); 99030f712c9SDave Chinner ents2 = dp->d_ops->leaf_ents_p(leaf2); 99130f712c9SDave Chinner 99230f712c9SDave Chinner oldsum = hdr1.count + hdr2.count; 99330f712c9SDave Chinner #if defined(DEBUG) || defined(XFS_WARN) 99430f712c9SDave Chinner oldstale = hdr1.stale + hdr2.stale; 99530f712c9SDave Chinner #endif 99630f712c9SDave Chinner mid = oldsum >> 1; 99730f712c9SDave Chinner 99830f712c9SDave Chinner /* 99930f712c9SDave Chinner * If the old leaf count was odd then the new one will be even, 100030f712c9SDave Chinner * so we need to divide the new count evenly. 100130f712c9SDave Chinner */ 100230f712c9SDave Chinner if (oldsum & 1) { 100330f712c9SDave Chinner xfs_dahash_t midhash; /* middle entry hash value */ 100430f712c9SDave Chinner 100530f712c9SDave Chinner if (mid >= hdr1.count) 100630f712c9SDave Chinner midhash = be32_to_cpu(ents2[mid - hdr1.count].hashval); 100730f712c9SDave Chinner else 100830f712c9SDave Chinner midhash = be32_to_cpu(ents1[mid].hashval); 100930f712c9SDave Chinner isleft = args->hashval <= midhash; 101030f712c9SDave Chinner } 101130f712c9SDave Chinner /* 101230f712c9SDave Chinner * If the old count is even then the new count is odd, so there's 101330f712c9SDave Chinner * no preferred side for the new entry. 101430f712c9SDave Chinner * Pick the left one. 101530f712c9SDave Chinner */ 101630f712c9SDave Chinner else 101730f712c9SDave Chinner isleft = 1; 101830f712c9SDave Chinner /* 101930f712c9SDave Chinner * Calculate moved entry count. Positive means left-to-right, 102030f712c9SDave Chinner * negative means right-to-left. Then move the entries. 102130f712c9SDave Chinner */ 102230f712c9SDave Chinner count = hdr1.count - mid + (isleft == 0); 102330f712c9SDave Chinner if (count > 0) 102430f712c9SDave Chinner xfs_dir3_leafn_moveents(args, blk1->bp, &hdr1, ents1, 102530f712c9SDave Chinner hdr1.count - count, blk2->bp, 102630f712c9SDave Chinner &hdr2, ents2, 0, count); 102730f712c9SDave Chinner else if (count < 0) 102830f712c9SDave Chinner xfs_dir3_leafn_moveents(args, blk2->bp, &hdr2, ents2, 0, 102930f712c9SDave Chinner blk1->bp, &hdr1, ents1, 103030f712c9SDave Chinner hdr1.count, count); 103130f712c9SDave Chinner 103230f712c9SDave Chinner ASSERT(hdr1.count + hdr2.count == oldsum); 103330f712c9SDave Chinner ASSERT(hdr1.stale + hdr2.stale == oldstale); 103430f712c9SDave Chinner 103530f712c9SDave Chinner /* log the changes made when moving the entries */ 103630f712c9SDave Chinner dp->d_ops->leaf_hdr_to_disk(leaf1, &hdr1); 103730f712c9SDave Chinner dp->d_ops->leaf_hdr_to_disk(leaf2, &hdr2); 103830f712c9SDave Chinner xfs_dir3_leaf_log_header(args, blk1->bp); 103930f712c9SDave Chinner xfs_dir3_leaf_log_header(args, blk2->bp); 104030f712c9SDave Chinner 104130f712c9SDave Chinner xfs_dir3_leaf_check(dp, blk1->bp); 104230f712c9SDave Chinner xfs_dir3_leaf_check(dp, blk2->bp); 104330f712c9SDave Chinner 104430f712c9SDave Chinner /* 104530f712c9SDave Chinner * Mark whether we're inserting into the old or new leaf. 104630f712c9SDave Chinner */ 104730f712c9SDave Chinner if (hdr1.count < hdr2.count) 104830f712c9SDave Chinner state->inleaf = swap; 104930f712c9SDave Chinner else if (hdr1.count > hdr2.count) 105030f712c9SDave Chinner state->inleaf = !swap; 105130f712c9SDave Chinner else 105230f712c9SDave Chinner state->inleaf = swap ^ (blk1->index <= hdr1.count); 105330f712c9SDave Chinner /* 105430f712c9SDave Chinner * Adjust the expected index for insertion. 105530f712c9SDave Chinner */ 105630f712c9SDave Chinner if (!state->inleaf) 105730f712c9SDave Chinner blk2->index = blk1->index - hdr1.count; 105830f712c9SDave Chinner 105930f712c9SDave Chinner /* 106030f712c9SDave Chinner * Finally sanity check just to make sure we are not returning a 106130f712c9SDave Chinner * negative index 106230f712c9SDave Chinner */ 106330f712c9SDave Chinner if (blk2->index < 0) { 106430f712c9SDave Chinner state->inleaf = 1; 106530f712c9SDave Chinner blk2->index = 0; 106630f712c9SDave Chinner xfs_alert(dp->i_mount, 106730f712c9SDave Chinner "%s: picked the wrong leaf? reverting original leaf: blk1->index %d", 106830f712c9SDave Chinner __func__, blk1->index); 106930f712c9SDave Chinner } 107030f712c9SDave Chinner } 107130f712c9SDave Chinner 107230f712c9SDave Chinner static int 107330f712c9SDave Chinner xfs_dir3_data_block_free( 107430f712c9SDave Chinner xfs_da_args_t *args, 107530f712c9SDave Chinner struct xfs_dir2_data_hdr *hdr, 107630f712c9SDave Chinner struct xfs_dir2_free *free, 107730f712c9SDave Chinner xfs_dir2_db_t fdb, 107830f712c9SDave Chinner int findex, 107930f712c9SDave Chinner struct xfs_buf *fbp, 108030f712c9SDave Chinner int longest) 108130f712c9SDave Chinner { 108230f712c9SDave Chinner int logfree = 0; 108330f712c9SDave Chinner __be16 *bests; 108430f712c9SDave Chinner struct xfs_dir3_icfree_hdr freehdr; 108530f712c9SDave Chinner struct xfs_inode *dp = args->dp; 108630f712c9SDave Chinner 108730f712c9SDave Chinner dp->d_ops->free_hdr_from_disk(&freehdr, free); 108830f712c9SDave Chinner bests = dp->d_ops->free_bests_p(free); 108930f712c9SDave Chinner if (hdr) { 109030f712c9SDave Chinner /* 109130f712c9SDave Chinner * Data block is not empty, just set the free entry to the new 109230f712c9SDave Chinner * value. 109330f712c9SDave Chinner */ 109430f712c9SDave Chinner bests[findex] = cpu_to_be16(longest); 109530f712c9SDave Chinner xfs_dir2_free_log_bests(args, fbp, findex, findex); 109630f712c9SDave Chinner return 0; 109730f712c9SDave Chinner } 109830f712c9SDave Chinner 109930f712c9SDave Chinner /* One less used entry in the free table. */ 110030f712c9SDave Chinner freehdr.nused--; 110130f712c9SDave Chinner 110230f712c9SDave Chinner /* 110330f712c9SDave Chinner * If this was the last entry in the table, we can trim the table size 110430f712c9SDave Chinner * back. There might be other entries at the end referring to 110530f712c9SDave Chinner * non-existent data blocks, get those too. 110630f712c9SDave Chinner */ 110730f712c9SDave Chinner if (findex == freehdr.nvalid - 1) { 110830f712c9SDave Chinner int i; /* free entry index */ 110930f712c9SDave Chinner 111030f712c9SDave Chinner for (i = findex - 1; i >= 0; i--) { 111130f712c9SDave Chinner if (bests[i] != cpu_to_be16(NULLDATAOFF)) 111230f712c9SDave Chinner break; 111330f712c9SDave Chinner } 111430f712c9SDave Chinner freehdr.nvalid = i + 1; 111530f712c9SDave Chinner logfree = 0; 111630f712c9SDave Chinner } else { 111730f712c9SDave Chinner /* Not the last entry, just punch it out. */ 111830f712c9SDave Chinner bests[findex] = cpu_to_be16(NULLDATAOFF); 111930f712c9SDave Chinner logfree = 1; 112030f712c9SDave Chinner } 112130f712c9SDave Chinner 112230f712c9SDave Chinner dp->d_ops->free_hdr_to_disk(free, &freehdr); 112330f712c9SDave Chinner xfs_dir2_free_log_header(args, fbp); 112430f712c9SDave Chinner 112530f712c9SDave Chinner /* 112630f712c9SDave Chinner * If there are no useful entries left in the block, get rid of the 112730f712c9SDave Chinner * block if we can. 112830f712c9SDave Chinner */ 112930f712c9SDave Chinner if (!freehdr.nused) { 113030f712c9SDave Chinner int error; 113130f712c9SDave Chinner 113230f712c9SDave Chinner error = xfs_dir2_shrink_inode(args, fdb, fbp); 113330f712c9SDave Chinner if (error == 0) { 113430f712c9SDave Chinner fbp = NULL; 113530f712c9SDave Chinner logfree = 0; 1136*2451337dSDave Chinner } else if (error != -ENOSPC || args->total != 0) 113730f712c9SDave Chinner return error; 113830f712c9SDave Chinner /* 113930f712c9SDave Chinner * It's possible to get ENOSPC if there is no 114030f712c9SDave Chinner * space reservation. In this case some one 114130f712c9SDave Chinner * else will eventually get rid of this block. 114230f712c9SDave Chinner */ 114330f712c9SDave Chinner } 114430f712c9SDave Chinner 114530f712c9SDave Chinner /* Log the free entry that changed, unless we got rid of it. */ 114630f712c9SDave Chinner if (logfree) 114730f712c9SDave Chinner xfs_dir2_free_log_bests(args, fbp, findex, findex); 114830f712c9SDave Chinner return 0; 114930f712c9SDave Chinner } 115030f712c9SDave Chinner 115130f712c9SDave Chinner /* 115230f712c9SDave Chinner * Remove an entry from a node directory. 115330f712c9SDave Chinner * This removes the leaf entry and the data entry, 115430f712c9SDave Chinner * and updates the free block if necessary. 115530f712c9SDave Chinner */ 115630f712c9SDave Chinner static int /* error */ 115730f712c9SDave Chinner xfs_dir2_leafn_remove( 115830f712c9SDave Chinner xfs_da_args_t *args, /* operation arguments */ 115930f712c9SDave Chinner struct xfs_buf *bp, /* leaf buffer */ 116030f712c9SDave Chinner int index, /* leaf entry index */ 116130f712c9SDave Chinner xfs_da_state_blk_t *dblk, /* data block */ 116230f712c9SDave Chinner int *rval) /* resulting block needs join */ 116330f712c9SDave Chinner { 116430f712c9SDave Chinner xfs_dir2_data_hdr_t *hdr; /* data block header */ 116530f712c9SDave Chinner xfs_dir2_db_t db; /* data block number */ 116630f712c9SDave Chinner struct xfs_buf *dbp; /* data block buffer */ 116730f712c9SDave Chinner xfs_dir2_data_entry_t *dep; /* data block entry */ 116830f712c9SDave Chinner xfs_inode_t *dp; /* incore directory inode */ 116930f712c9SDave Chinner xfs_dir2_leaf_t *leaf; /* leaf structure */ 117030f712c9SDave Chinner xfs_dir2_leaf_entry_t *lep; /* leaf entry */ 117130f712c9SDave Chinner int longest; /* longest data free entry */ 117230f712c9SDave Chinner int off; /* data block entry offset */ 117330f712c9SDave Chinner xfs_mount_t *mp; /* filesystem mount point */ 117430f712c9SDave Chinner int needlog; /* need to log data header */ 117530f712c9SDave Chinner int needscan; /* need to rescan data frees */ 117630f712c9SDave Chinner xfs_trans_t *tp; /* transaction pointer */ 117730f712c9SDave Chinner struct xfs_dir2_data_free *bf; /* bestfree table */ 117830f712c9SDave Chinner struct xfs_dir3_icleaf_hdr leafhdr; 117930f712c9SDave Chinner struct xfs_dir2_leaf_entry *ents; 118030f712c9SDave Chinner 118130f712c9SDave Chinner trace_xfs_dir2_leafn_remove(args, index); 118230f712c9SDave Chinner 118330f712c9SDave Chinner dp = args->dp; 118430f712c9SDave Chinner tp = args->trans; 118530f712c9SDave Chinner mp = dp->i_mount; 118630f712c9SDave Chinner leaf = bp->b_addr; 118730f712c9SDave Chinner dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf); 118830f712c9SDave Chinner ents = dp->d_ops->leaf_ents_p(leaf); 118930f712c9SDave Chinner 119030f712c9SDave Chinner /* 119130f712c9SDave Chinner * Point to the entry we're removing. 119230f712c9SDave Chinner */ 119330f712c9SDave Chinner lep = &ents[index]; 119430f712c9SDave Chinner 119530f712c9SDave Chinner /* 119630f712c9SDave Chinner * Extract the data block and offset from the entry. 119730f712c9SDave Chinner */ 119830f712c9SDave Chinner db = xfs_dir2_dataptr_to_db(args->geo, be32_to_cpu(lep->address)); 119930f712c9SDave Chinner ASSERT(dblk->blkno == db); 120030f712c9SDave Chinner off = xfs_dir2_dataptr_to_off(args->geo, be32_to_cpu(lep->address)); 120130f712c9SDave Chinner ASSERT(dblk->index == off); 120230f712c9SDave Chinner 120330f712c9SDave Chinner /* 120430f712c9SDave Chinner * Kill the leaf entry by marking it stale. 120530f712c9SDave Chinner * Log the leaf block changes. 120630f712c9SDave Chinner */ 120730f712c9SDave Chinner leafhdr.stale++; 120830f712c9SDave Chinner dp->d_ops->leaf_hdr_to_disk(leaf, &leafhdr); 120930f712c9SDave Chinner xfs_dir3_leaf_log_header(args, bp); 121030f712c9SDave Chinner 121130f712c9SDave Chinner lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR); 121230f712c9SDave Chinner xfs_dir3_leaf_log_ents(args, bp, index, index); 121330f712c9SDave Chinner 121430f712c9SDave Chinner /* 121530f712c9SDave Chinner * Make the data entry free. Keep track of the longest freespace 121630f712c9SDave Chinner * in the data block in case it changes. 121730f712c9SDave Chinner */ 121830f712c9SDave Chinner dbp = dblk->bp; 121930f712c9SDave Chinner hdr = dbp->b_addr; 122030f712c9SDave Chinner dep = (xfs_dir2_data_entry_t *)((char *)hdr + off); 122130f712c9SDave Chinner bf = dp->d_ops->data_bestfree_p(hdr); 122230f712c9SDave Chinner longest = be16_to_cpu(bf[0].length); 122330f712c9SDave Chinner needlog = needscan = 0; 122430f712c9SDave Chinner xfs_dir2_data_make_free(args, dbp, off, 122530f712c9SDave Chinner dp->d_ops->data_entsize(dep->namelen), &needlog, &needscan); 122630f712c9SDave Chinner /* 122730f712c9SDave Chinner * Rescan the data block freespaces for bestfree. 122830f712c9SDave Chinner * Log the data block header if needed. 122930f712c9SDave Chinner */ 123030f712c9SDave Chinner if (needscan) 123130f712c9SDave Chinner xfs_dir2_data_freescan(dp, hdr, &needlog); 123230f712c9SDave Chinner if (needlog) 123330f712c9SDave Chinner xfs_dir2_data_log_header(args, dbp); 123430f712c9SDave Chinner xfs_dir3_data_check(dp, dbp); 123530f712c9SDave Chinner /* 123630f712c9SDave Chinner * If the longest data block freespace changes, need to update 123730f712c9SDave Chinner * the corresponding freeblock entry. 123830f712c9SDave Chinner */ 123930f712c9SDave Chinner if (longest < be16_to_cpu(bf[0].length)) { 124030f712c9SDave Chinner int error; /* error return value */ 124130f712c9SDave Chinner struct xfs_buf *fbp; /* freeblock buffer */ 124230f712c9SDave Chinner xfs_dir2_db_t fdb; /* freeblock block number */ 124330f712c9SDave Chinner int findex; /* index in freeblock entries */ 124430f712c9SDave Chinner xfs_dir2_free_t *free; /* freeblock structure */ 124530f712c9SDave Chinner 124630f712c9SDave Chinner /* 124730f712c9SDave Chinner * Convert the data block number to a free block, 124830f712c9SDave Chinner * read in the free block. 124930f712c9SDave Chinner */ 125030f712c9SDave Chinner fdb = dp->d_ops->db_to_fdb(args->geo, db); 125130f712c9SDave Chinner error = xfs_dir2_free_read(tp, dp, 125230f712c9SDave Chinner xfs_dir2_db_to_da(args->geo, fdb), 125330f712c9SDave Chinner &fbp); 125430f712c9SDave Chinner if (error) 125530f712c9SDave Chinner return error; 125630f712c9SDave Chinner free = fbp->b_addr; 125730f712c9SDave Chinner #ifdef DEBUG 125830f712c9SDave Chinner { 125930f712c9SDave Chinner struct xfs_dir3_icfree_hdr freehdr; 126030f712c9SDave Chinner dp->d_ops->free_hdr_from_disk(&freehdr, free); 126130f712c9SDave Chinner ASSERT(freehdr.firstdb == dp->d_ops->free_max_bests(args->geo) * 126230f712c9SDave Chinner (fdb - xfs_dir2_byte_to_db(args->geo, 126330f712c9SDave Chinner XFS_DIR2_FREE_OFFSET))); 126430f712c9SDave Chinner } 126530f712c9SDave Chinner #endif 126630f712c9SDave Chinner /* 126730f712c9SDave Chinner * Calculate which entry we need to fix. 126830f712c9SDave Chinner */ 126930f712c9SDave Chinner findex = dp->d_ops->db_to_fdindex(args->geo, db); 127030f712c9SDave Chinner longest = be16_to_cpu(bf[0].length); 127130f712c9SDave Chinner /* 127230f712c9SDave Chinner * If the data block is now empty we can get rid of it 127330f712c9SDave Chinner * (usually). 127430f712c9SDave Chinner */ 127530f712c9SDave Chinner if (longest == args->geo->blksize - 127630f712c9SDave Chinner dp->d_ops->data_entry_offset) { 127730f712c9SDave Chinner /* 127830f712c9SDave Chinner * Try to punch out the data block. 127930f712c9SDave Chinner */ 128030f712c9SDave Chinner error = xfs_dir2_shrink_inode(args, db, dbp); 128130f712c9SDave Chinner if (error == 0) { 128230f712c9SDave Chinner dblk->bp = NULL; 128330f712c9SDave Chinner hdr = NULL; 128430f712c9SDave Chinner } 128530f712c9SDave Chinner /* 128630f712c9SDave Chinner * We can get ENOSPC if there's no space reservation. 128730f712c9SDave Chinner * In this case just drop the buffer and some one else 128830f712c9SDave Chinner * will eventually get rid of the empty block. 128930f712c9SDave Chinner */ 1290*2451337dSDave Chinner else if (!(error == -ENOSPC && args->total == 0)) 129130f712c9SDave Chinner return error; 129230f712c9SDave Chinner } 129330f712c9SDave Chinner /* 129430f712c9SDave Chinner * If we got rid of the data block, we can eliminate that entry 129530f712c9SDave Chinner * in the free block. 129630f712c9SDave Chinner */ 129730f712c9SDave Chinner error = xfs_dir3_data_block_free(args, hdr, free, 129830f712c9SDave Chinner fdb, findex, fbp, longest); 129930f712c9SDave Chinner if (error) 130030f712c9SDave Chinner return error; 130130f712c9SDave Chinner } 130230f712c9SDave Chinner 130330f712c9SDave Chinner xfs_dir3_leaf_check(dp, bp); 130430f712c9SDave Chinner /* 130530f712c9SDave Chinner * Return indication of whether this leaf block is empty enough 130630f712c9SDave Chinner * to justify trying to join it with a neighbor. 130730f712c9SDave Chinner */ 130830f712c9SDave Chinner *rval = (dp->d_ops->leaf_hdr_size + 130930f712c9SDave Chinner (uint)sizeof(ents[0]) * (leafhdr.count - leafhdr.stale)) < 131030f712c9SDave Chinner args->geo->magicpct; 131130f712c9SDave Chinner return 0; 131230f712c9SDave Chinner } 131330f712c9SDave Chinner 131430f712c9SDave Chinner /* 131530f712c9SDave Chinner * Split the leaf entries in the old block into old and new blocks. 131630f712c9SDave Chinner */ 131730f712c9SDave Chinner int /* error */ 131830f712c9SDave Chinner xfs_dir2_leafn_split( 131930f712c9SDave Chinner xfs_da_state_t *state, /* btree cursor */ 132030f712c9SDave Chinner xfs_da_state_blk_t *oldblk, /* original block */ 132130f712c9SDave Chinner xfs_da_state_blk_t *newblk) /* newly created block */ 132230f712c9SDave Chinner { 132330f712c9SDave Chinner xfs_da_args_t *args; /* operation arguments */ 132430f712c9SDave Chinner xfs_dablk_t blkno; /* new leaf block number */ 132530f712c9SDave Chinner int error; /* error return value */ 132630f712c9SDave Chinner xfs_mount_t *mp; /* filesystem mount point */ 132730f712c9SDave Chinner struct xfs_inode *dp; 132830f712c9SDave Chinner 132930f712c9SDave Chinner /* 133030f712c9SDave Chinner * Allocate space for a new leaf node. 133130f712c9SDave Chinner */ 133230f712c9SDave Chinner args = state->args; 133330f712c9SDave Chinner dp = args->dp; 133430f712c9SDave Chinner mp = dp->i_mount; 133530f712c9SDave Chinner ASSERT(oldblk->magic == XFS_DIR2_LEAFN_MAGIC); 133630f712c9SDave Chinner error = xfs_da_grow_inode(args, &blkno); 133730f712c9SDave Chinner if (error) { 133830f712c9SDave Chinner return error; 133930f712c9SDave Chinner } 134030f712c9SDave Chinner /* 134130f712c9SDave Chinner * Initialize the new leaf block. 134230f712c9SDave Chinner */ 134330f712c9SDave Chinner error = xfs_dir3_leaf_get_buf(args, xfs_dir2_da_to_db(args->geo, blkno), 134430f712c9SDave Chinner &newblk->bp, XFS_DIR2_LEAFN_MAGIC); 134530f712c9SDave Chinner if (error) 134630f712c9SDave Chinner return error; 134730f712c9SDave Chinner 134830f712c9SDave Chinner newblk->blkno = blkno; 134930f712c9SDave Chinner newblk->magic = XFS_DIR2_LEAFN_MAGIC; 135030f712c9SDave Chinner /* 135130f712c9SDave Chinner * Rebalance the entries across the two leaves, link the new 135230f712c9SDave Chinner * block into the leaves. 135330f712c9SDave Chinner */ 135430f712c9SDave Chinner xfs_dir2_leafn_rebalance(state, oldblk, newblk); 135530f712c9SDave Chinner error = xfs_da3_blk_link(state, oldblk, newblk); 135630f712c9SDave Chinner if (error) { 135730f712c9SDave Chinner return error; 135830f712c9SDave Chinner } 135930f712c9SDave Chinner /* 136030f712c9SDave Chinner * Insert the new entry in the correct block. 136130f712c9SDave Chinner */ 136230f712c9SDave Chinner if (state->inleaf) 136330f712c9SDave Chinner error = xfs_dir2_leafn_add(oldblk->bp, args, oldblk->index); 136430f712c9SDave Chinner else 136530f712c9SDave Chinner error = xfs_dir2_leafn_add(newblk->bp, args, newblk->index); 136630f712c9SDave Chinner /* 136730f712c9SDave Chinner * Update last hashval in each block since we added the name. 136830f712c9SDave Chinner */ 136930f712c9SDave Chinner oldblk->hashval = xfs_dir2_leafn_lasthash(dp, oldblk->bp, NULL); 137030f712c9SDave Chinner newblk->hashval = xfs_dir2_leafn_lasthash(dp, newblk->bp, NULL); 137130f712c9SDave Chinner xfs_dir3_leaf_check(dp, oldblk->bp); 137230f712c9SDave Chinner xfs_dir3_leaf_check(dp, newblk->bp); 137330f712c9SDave Chinner return error; 137430f712c9SDave Chinner } 137530f712c9SDave Chinner 137630f712c9SDave Chinner /* 137730f712c9SDave Chinner * Check a leaf block and its neighbors to see if the block should be 137830f712c9SDave Chinner * collapsed into one or the other neighbor. Always keep the block 137930f712c9SDave Chinner * with the smaller block number. 138030f712c9SDave Chinner * If the current block is over 50% full, don't try to join it, return 0. 138130f712c9SDave Chinner * If the block is empty, fill in the state structure and return 2. 138230f712c9SDave Chinner * If it can be collapsed, fill in the state structure and return 1. 138330f712c9SDave Chinner * If nothing can be done, return 0. 138430f712c9SDave Chinner */ 138530f712c9SDave Chinner int /* error */ 138630f712c9SDave Chinner xfs_dir2_leafn_toosmall( 138730f712c9SDave Chinner xfs_da_state_t *state, /* btree cursor */ 138830f712c9SDave Chinner int *action) /* resulting action to take */ 138930f712c9SDave Chinner { 139030f712c9SDave Chinner xfs_da_state_blk_t *blk; /* leaf block */ 139130f712c9SDave Chinner xfs_dablk_t blkno; /* leaf block number */ 139230f712c9SDave Chinner struct xfs_buf *bp; /* leaf buffer */ 139330f712c9SDave Chinner int bytes; /* bytes in use */ 139430f712c9SDave Chinner int count; /* leaf live entry count */ 139530f712c9SDave Chinner int error; /* error return value */ 139630f712c9SDave Chinner int forward; /* sibling block direction */ 139730f712c9SDave Chinner int i; /* sibling counter */ 139830f712c9SDave Chinner xfs_dir2_leaf_t *leaf; /* leaf structure */ 139930f712c9SDave Chinner int rval; /* result from path_shift */ 140030f712c9SDave Chinner struct xfs_dir3_icleaf_hdr leafhdr; 140130f712c9SDave Chinner struct xfs_dir2_leaf_entry *ents; 140230f712c9SDave Chinner struct xfs_inode *dp = state->args->dp; 140330f712c9SDave Chinner 140430f712c9SDave Chinner /* 140530f712c9SDave Chinner * Check for the degenerate case of the block being over 50% full. 140630f712c9SDave Chinner * If so, it's not worth even looking to see if we might be able 140730f712c9SDave Chinner * to coalesce with a sibling. 140830f712c9SDave Chinner */ 140930f712c9SDave Chinner blk = &state->path.blk[state->path.active - 1]; 141030f712c9SDave Chinner leaf = blk->bp->b_addr; 141130f712c9SDave Chinner dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf); 141230f712c9SDave Chinner ents = dp->d_ops->leaf_ents_p(leaf); 141330f712c9SDave Chinner xfs_dir3_leaf_check(dp, blk->bp); 141430f712c9SDave Chinner 141530f712c9SDave Chinner count = leafhdr.count - leafhdr.stale; 141630f712c9SDave Chinner bytes = dp->d_ops->leaf_hdr_size + count * sizeof(ents[0]); 141730f712c9SDave Chinner if (bytes > (state->args->geo->blksize >> 1)) { 141830f712c9SDave Chinner /* 141930f712c9SDave Chinner * Blk over 50%, don't try to join. 142030f712c9SDave Chinner */ 142130f712c9SDave Chinner *action = 0; 142230f712c9SDave Chinner return 0; 142330f712c9SDave Chinner } 142430f712c9SDave Chinner /* 142530f712c9SDave Chinner * Check for the degenerate case of the block being empty. 142630f712c9SDave Chinner * If the block is empty, we'll simply delete it, no need to 142730f712c9SDave Chinner * coalesce it with a sibling block. We choose (arbitrarily) 142830f712c9SDave Chinner * to merge with the forward block unless it is NULL. 142930f712c9SDave Chinner */ 143030f712c9SDave Chinner if (count == 0) { 143130f712c9SDave Chinner /* 143230f712c9SDave Chinner * Make altpath point to the block we want to keep and 143330f712c9SDave Chinner * path point to the block we want to drop (this one). 143430f712c9SDave Chinner */ 143530f712c9SDave Chinner forward = (leafhdr.forw != 0); 143630f712c9SDave Chinner memcpy(&state->altpath, &state->path, sizeof(state->path)); 143730f712c9SDave Chinner error = xfs_da3_path_shift(state, &state->altpath, forward, 0, 143830f712c9SDave Chinner &rval); 143930f712c9SDave Chinner if (error) 144030f712c9SDave Chinner return error; 144130f712c9SDave Chinner *action = rval ? 2 : 0; 144230f712c9SDave Chinner return 0; 144330f712c9SDave Chinner } 144430f712c9SDave Chinner /* 144530f712c9SDave Chinner * Examine each sibling block to see if we can coalesce with 144630f712c9SDave Chinner * at least 25% free space to spare. We need to figure out 144730f712c9SDave Chinner * whether to merge with the forward or the backward block. 144830f712c9SDave Chinner * We prefer coalescing with the lower numbered sibling so as 144930f712c9SDave Chinner * to shrink a directory over time. 145030f712c9SDave Chinner */ 145130f712c9SDave Chinner forward = leafhdr.forw < leafhdr.back; 145230f712c9SDave Chinner for (i = 0, bp = NULL; i < 2; forward = !forward, i++) { 145330f712c9SDave Chinner struct xfs_dir3_icleaf_hdr hdr2; 145430f712c9SDave Chinner 145530f712c9SDave Chinner blkno = forward ? leafhdr.forw : leafhdr.back; 145630f712c9SDave Chinner if (blkno == 0) 145730f712c9SDave Chinner continue; 145830f712c9SDave Chinner /* 145930f712c9SDave Chinner * Read the sibling leaf block. 146030f712c9SDave Chinner */ 146130f712c9SDave Chinner error = xfs_dir3_leafn_read(state->args->trans, dp, 146230f712c9SDave Chinner blkno, -1, &bp); 146330f712c9SDave Chinner if (error) 146430f712c9SDave Chinner return error; 146530f712c9SDave Chinner 146630f712c9SDave Chinner /* 146730f712c9SDave Chinner * Count bytes in the two blocks combined. 146830f712c9SDave Chinner */ 146930f712c9SDave Chinner count = leafhdr.count - leafhdr.stale; 147030f712c9SDave Chinner bytes = state->args->geo->blksize - 147130f712c9SDave Chinner (state->args->geo->blksize >> 2); 147230f712c9SDave Chinner 147330f712c9SDave Chinner leaf = bp->b_addr; 147430f712c9SDave Chinner dp->d_ops->leaf_hdr_from_disk(&hdr2, leaf); 147530f712c9SDave Chinner ents = dp->d_ops->leaf_ents_p(leaf); 147630f712c9SDave Chinner count += hdr2.count - hdr2.stale; 147730f712c9SDave Chinner bytes -= count * sizeof(ents[0]); 147830f712c9SDave Chinner 147930f712c9SDave Chinner /* 148030f712c9SDave Chinner * Fits with at least 25% to spare. 148130f712c9SDave Chinner */ 148230f712c9SDave Chinner if (bytes >= 0) 148330f712c9SDave Chinner break; 148430f712c9SDave Chinner xfs_trans_brelse(state->args->trans, bp); 148530f712c9SDave Chinner } 148630f712c9SDave Chinner /* 148730f712c9SDave Chinner * Didn't like either block, give up. 148830f712c9SDave Chinner */ 148930f712c9SDave Chinner if (i >= 2) { 149030f712c9SDave Chinner *action = 0; 149130f712c9SDave Chinner return 0; 149230f712c9SDave Chinner } 149330f712c9SDave Chinner 149430f712c9SDave Chinner /* 149530f712c9SDave Chinner * Make altpath point to the block we want to keep (the lower 149630f712c9SDave Chinner * numbered block) and path point to the block we want to drop. 149730f712c9SDave Chinner */ 149830f712c9SDave Chinner memcpy(&state->altpath, &state->path, sizeof(state->path)); 149930f712c9SDave Chinner if (blkno < blk->blkno) 150030f712c9SDave Chinner error = xfs_da3_path_shift(state, &state->altpath, forward, 0, 150130f712c9SDave Chinner &rval); 150230f712c9SDave Chinner else 150330f712c9SDave Chinner error = xfs_da3_path_shift(state, &state->path, forward, 0, 150430f712c9SDave Chinner &rval); 150530f712c9SDave Chinner if (error) { 150630f712c9SDave Chinner return error; 150730f712c9SDave Chinner } 150830f712c9SDave Chinner *action = rval ? 0 : 1; 150930f712c9SDave Chinner return 0; 151030f712c9SDave Chinner } 151130f712c9SDave Chinner 151230f712c9SDave Chinner /* 151330f712c9SDave Chinner * Move all the leaf entries from drop_blk to save_blk. 151430f712c9SDave Chinner * This is done as part of a join operation. 151530f712c9SDave Chinner */ 151630f712c9SDave Chinner void 151730f712c9SDave Chinner xfs_dir2_leafn_unbalance( 151830f712c9SDave Chinner xfs_da_state_t *state, /* cursor */ 151930f712c9SDave Chinner xfs_da_state_blk_t *drop_blk, /* dead block */ 152030f712c9SDave Chinner xfs_da_state_blk_t *save_blk) /* surviving block */ 152130f712c9SDave Chinner { 152230f712c9SDave Chinner xfs_da_args_t *args; /* operation arguments */ 152330f712c9SDave Chinner xfs_dir2_leaf_t *drop_leaf; /* dead leaf structure */ 152430f712c9SDave Chinner xfs_dir2_leaf_t *save_leaf; /* surviving leaf structure */ 152530f712c9SDave Chinner struct xfs_dir3_icleaf_hdr savehdr; 152630f712c9SDave Chinner struct xfs_dir3_icleaf_hdr drophdr; 152730f712c9SDave Chinner struct xfs_dir2_leaf_entry *sents; 152830f712c9SDave Chinner struct xfs_dir2_leaf_entry *dents; 152930f712c9SDave Chinner struct xfs_inode *dp = state->args->dp; 153030f712c9SDave Chinner 153130f712c9SDave Chinner args = state->args; 153230f712c9SDave Chinner ASSERT(drop_blk->magic == XFS_DIR2_LEAFN_MAGIC); 153330f712c9SDave Chinner ASSERT(save_blk->magic == XFS_DIR2_LEAFN_MAGIC); 153430f712c9SDave Chinner drop_leaf = drop_blk->bp->b_addr; 153530f712c9SDave Chinner save_leaf = save_blk->bp->b_addr; 153630f712c9SDave Chinner 153730f712c9SDave Chinner dp->d_ops->leaf_hdr_from_disk(&savehdr, save_leaf); 153830f712c9SDave Chinner dp->d_ops->leaf_hdr_from_disk(&drophdr, drop_leaf); 153930f712c9SDave Chinner sents = dp->d_ops->leaf_ents_p(save_leaf); 154030f712c9SDave Chinner dents = dp->d_ops->leaf_ents_p(drop_leaf); 154130f712c9SDave Chinner 154230f712c9SDave Chinner /* 154330f712c9SDave Chinner * If there are any stale leaf entries, take this opportunity 154430f712c9SDave Chinner * to purge them. 154530f712c9SDave Chinner */ 154630f712c9SDave Chinner if (drophdr.stale) 154730f712c9SDave Chinner xfs_dir3_leaf_compact(args, &drophdr, drop_blk->bp); 154830f712c9SDave Chinner if (savehdr.stale) 154930f712c9SDave Chinner xfs_dir3_leaf_compact(args, &savehdr, save_blk->bp); 155030f712c9SDave Chinner 155130f712c9SDave Chinner /* 155230f712c9SDave Chinner * Move the entries from drop to the appropriate end of save. 155330f712c9SDave Chinner */ 155430f712c9SDave Chinner drop_blk->hashval = be32_to_cpu(dents[drophdr.count - 1].hashval); 155530f712c9SDave Chinner if (xfs_dir2_leafn_order(dp, save_blk->bp, drop_blk->bp)) 155630f712c9SDave Chinner xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0, 155730f712c9SDave Chinner save_blk->bp, &savehdr, sents, 0, 155830f712c9SDave Chinner drophdr.count); 155930f712c9SDave Chinner else 156030f712c9SDave Chinner xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0, 156130f712c9SDave Chinner save_blk->bp, &savehdr, sents, 156230f712c9SDave Chinner savehdr.count, drophdr.count); 156330f712c9SDave Chinner save_blk->hashval = be32_to_cpu(sents[savehdr.count - 1].hashval); 156430f712c9SDave Chinner 156530f712c9SDave Chinner /* log the changes made when moving the entries */ 156630f712c9SDave Chinner dp->d_ops->leaf_hdr_to_disk(save_leaf, &savehdr); 156730f712c9SDave Chinner dp->d_ops->leaf_hdr_to_disk(drop_leaf, &drophdr); 156830f712c9SDave Chinner xfs_dir3_leaf_log_header(args, save_blk->bp); 156930f712c9SDave Chinner xfs_dir3_leaf_log_header(args, drop_blk->bp); 157030f712c9SDave Chinner 157130f712c9SDave Chinner xfs_dir3_leaf_check(dp, save_blk->bp); 157230f712c9SDave Chinner xfs_dir3_leaf_check(dp, drop_blk->bp); 157330f712c9SDave Chinner } 157430f712c9SDave Chinner 157530f712c9SDave Chinner /* 157630f712c9SDave Chinner * Top-level node form directory addname routine. 157730f712c9SDave Chinner */ 157830f712c9SDave Chinner int /* error */ 157930f712c9SDave Chinner xfs_dir2_node_addname( 158030f712c9SDave Chinner xfs_da_args_t *args) /* operation arguments */ 158130f712c9SDave Chinner { 158230f712c9SDave Chinner xfs_da_state_blk_t *blk; /* leaf block for insert */ 158330f712c9SDave Chinner int error; /* error return value */ 158430f712c9SDave Chinner int rval; /* sub-return value */ 158530f712c9SDave Chinner xfs_da_state_t *state; /* btree cursor */ 158630f712c9SDave Chinner 158730f712c9SDave Chinner trace_xfs_dir2_node_addname(args); 158830f712c9SDave Chinner 158930f712c9SDave Chinner /* 159030f712c9SDave Chinner * Allocate and initialize the state (btree cursor). 159130f712c9SDave Chinner */ 159230f712c9SDave Chinner state = xfs_da_state_alloc(); 159330f712c9SDave Chinner state->args = args; 159430f712c9SDave Chinner state->mp = args->dp->i_mount; 159530f712c9SDave Chinner /* 159630f712c9SDave Chinner * Look up the name. We're not supposed to find it, but 159730f712c9SDave Chinner * this gives us the insertion point. 159830f712c9SDave Chinner */ 159930f712c9SDave Chinner error = xfs_da3_node_lookup_int(state, &rval); 160030f712c9SDave Chinner if (error) 160130f712c9SDave Chinner rval = error; 1602*2451337dSDave Chinner if (rval != -ENOENT) { 160330f712c9SDave Chinner goto done; 160430f712c9SDave Chinner } 160530f712c9SDave Chinner /* 160630f712c9SDave Chinner * Add the data entry to a data block. 160730f712c9SDave Chinner * Extravalid is set to a freeblock found by lookup. 160830f712c9SDave Chinner */ 160930f712c9SDave Chinner rval = xfs_dir2_node_addname_int(args, 161030f712c9SDave Chinner state->extravalid ? &state->extrablk : NULL); 161130f712c9SDave Chinner if (rval) { 161230f712c9SDave Chinner goto done; 161330f712c9SDave Chinner } 161430f712c9SDave Chinner blk = &state->path.blk[state->path.active - 1]; 161530f712c9SDave Chinner ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC); 161630f712c9SDave Chinner /* 161730f712c9SDave Chinner * Add the new leaf entry. 161830f712c9SDave Chinner */ 161930f712c9SDave Chinner rval = xfs_dir2_leafn_add(blk->bp, args, blk->index); 162030f712c9SDave Chinner if (rval == 0) { 162130f712c9SDave Chinner /* 162230f712c9SDave Chinner * It worked, fix the hash values up the btree. 162330f712c9SDave Chinner */ 162430f712c9SDave Chinner if (!(args->op_flags & XFS_DA_OP_JUSTCHECK)) 162530f712c9SDave Chinner xfs_da3_fixhashpath(state, &state->path); 162630f712c9SDave Chinner } else { 162730f712c9SDave Chinner /* 162830f712c9SDave Chinner * It didn't work, we need to split the leaf block. 162930f712c9SDave Chinner */ 163030f712c9SDave Chinner if (args->total == 0) { 1631*2451337dSDave Chinner ASSERT(rval == -ENOSPC); 163230f712c9SDave Chinner goto done; 163330f712c9SDave Chinner } 163430f712c9SDave Chinner /* 163530f712c9SDave Chinner * Split the leaf block and insert the new entry. 163630f712c9SDave Chinner */ 163730f712c9SDave Chinner rval = xfs_da3_split(state); 163830f712c9SDave Chinner } 163930f712c9SDave Chinner done: 164030f712c9SDave Chinner xfs_da_state_free(state); 164130f712c9SDave Chinner return rval; 164230f712c9SDave Chinner } 164330f712c9SDave Chinner 164430f712c9SDave Chinner /* 164530f712c9SDave Chinner * Add the data entry for a node-format directory name addition. 164630f712c9SDave Chinner * The leaf entry is added in xfs_dir2_leafn_add. 164730f712c9SDave Chinner * We may enter with a freespace block that the lookup found. 164830f712c9SDave Chinner */ 164930f712c9SDave Chinner static int /* error */ 165030f712c9SDave Chinner xfs_dir2_node_addname_int( 165130f712c9SDave Chinner xfs_da_args_t *args, /* operation arguments */ 165230f712c9SDave Chinner xfs_da_state_blk_t *fblk) /* optional freespace block */ 165330f712c9SDave Chinner { 165430f712c9SDave Chinner xfs_dir2_data_hdr_t *hdr; /* data block header */ 165530f712c9SDave Chinner xfs_dir2_db_t dbno; /* data block number */ 165630f712c9SDave Chinner struct xfs_buf *dbp; /* data block buffer */ 165730f712c9SDave Chinner xfs_dir2_data_entry_t *dep; /* data entry pointer */ 165830f712c9SDave Chinner xfs_inode_t *dp; /* incore directory inode */ 165930f712c9SDave Chinner xfs_dir2_data_unused_t *dup; /* data unused entry pointer */ 166030f712c9SDave Chinner int error; /* error return value */ 166130f712c9SDave Chinner xfs_dir2_db_t fbno; /* freespace block number */ 166230f712c9SDave Chinner struct xfs_buf *fbp; /* freespace buffer */ 166330f712c9SDave Chinner int findex; /* freespace entry index */ 166430f712c9SDave Chinner xfs_dir2_free_t *free=NULL; /* freespace block structure */ 166530f712c9SDave Chinner xfs_dir2_db_t ifbno; /* initial freespace block no */ 166630f712c9SDave Chinner xfs_dir2_db_t lastfbno=0; /* highest freespace block no */ 166730f712c9SDave Chinner int length; /* length of the new entry */ 166830f712c9SDave Chinner int logfree; /* need to log free entry */ 166930f712c9SDave Chinner xfs_mount_t *mp; /* filesystem mount point */ 167030f712c9SDave Chinner int needlog; /* need to log data header */ 167130f712c9SDave Chinner int needscan; /* need to rescan data frees */ 167230f712c9SDave Chinner __be16 *tagp; /* data entry tag pointer */ 167330f712c9SDave Chinner xfs_trans_t *tp; /* transaction pointer */ 167430f712c9SDave Chinner __be16 *bests; 167530f712c9SDave Chinner struct xfs_dir3_icfree_hdr freehdr; 167630f712c9SDave Chinner struct xfs_dir2_data_free *bf; 167730f712c9SDave Chinner 167830f712c9SDave Chinner dp = args->dp; 167930f712c9SDave Chinner mp = dp->i_mount; 168030f712c9SDave Chinner tp = args->trans; 168130f712c9SDave Chinner length = dp->d_ops->data_entsize(args->namelen); 168230f712c9SDave Chinner /* 168330f712c9SDave Chinner * If we came in with a freespace block that means that lookup 168430f712c9SDave Chinner * found an entry with our hash value. This is the freespace 168530f712c9SDave Chinner * block for that data entry. 168630f712c9SDave Chinner */ 168730f712c9SDave Chinner if (fblk) { 168830f712c9SDave Chinner fbp = fblk->bp; 168930f712c9SDave Chinner /* 169030f712c9SDave Chinner * Remember initial freespace block number. 169130f712c9SDave Chinner */ 169230f712c9SDave Chinner ifbno = fblk->blkno; 169330f712c9SDave Chinner free = fbp->b_addr; 169430f712c9SDave Chinner findex = fblk->index; 169530f712c9SDave Chinner bests = dp->d_ops->free_bests_p(free); 169630f712c9SDave Chinner dp->d_ops->free_hdr_from_disk(&freehdr, free); 169730f712c9SDave Chinner 169830f712c9SDave Chinner /* 169930f712c9SDave Chinner * This means the free entry showed that the data block had 170030f712c9SDave Chinner * space for our entry, so we remembered it. 170130f712c9SDave Chinner * Use that data block. 170230f712c9SDave Chinner */ 170330f712c9SDave Chinner if (findex >= 0) { 170430f712c9SDave Chinner ASSERT(findex < freehdr.nvalid); 170530f712c9SDave Chinner ASSERT(be16_to_cpu(bests[findex]) != NULLDATAOFF); 170630f712c9SDave Chinner ASSERT(be16_to_cpu(bests[findex]) >= length); 170730f712c9SDave Chinner dbno = freehdr.firstdb + findex; 170830f712c9SDave Chinner } else { 170930f712c9SDave Chinner /* 171030f712c9SDave Chinner * The data block looked at didn't have enough room. 171130f712c9SDave Chinner * We'll start at the beginning of the freespace entries. 171230f712c9SDave Chinner */ 171330f712c9SDave Chinner dbno = -1; 171430f712c9SDave Chinner findex = 0; 171530f712c9SDave Chinner } 171630f712c9SDave Chinner } else { 171730f712c9SDave Chinner /* 171830f712c9SDave Chinner * Didn't come in with a freespace block, so no data block. 171930f712c9SDave Chinner */ 172030f712c9SDave Chinner ifbno = dbno = -1; 172130f712c9SDave Chinner fbp = NULL; 172230f712c9SDave Chinner findex = 0; 172330f712c9SDave Chinner } 172430f712c9SDave Chinner 172530f712c9SDave Chinner /* 172630f712c9SDave Chinner * If we don't have a data block yet, we're going to scan the 172730f712c9SDave Chinner * freespace blocks looking for one. Figure out what the 172830f712c9SDave Chinner * highest freespace block number is. 172930f712c9SDave Chinner */ 173030f712c9SDave Chinner if (dbno == -1) { 173130f712c9SDave Chinner xfs_fileoff_t fo; /* freespace block number */ 173230f712c9SDave Chinner 173330f712c9SDave Chinner if ((error = xfs_bmap_last_offset(dp, &fo, XFS_DATA_FORK))) 173430f712c9SDave Chinner return error; 173530f712c9SDave Chinner lastfbno = xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)fo); 173630f712c9SDave Chinner fbno = ifbno; 173730f712c9SDave Chinner } 173830f712c9SDave Chinner /* 173930f712c9SDave Chinner * While we haven't identified a data block, search the freeblock 174030f712c9SDave Chinner * data for a good data block. If we find a null freeblock entry, 174130f712c9SDave Chinner * indicating a hole in the data blocks, remember that. 174230f712c9SDave Chinner */ 174330f712c9SDave Chinner while (dbno == -1) { 174430f712c9SDave Chinner /* 174530f712c9SDave Chinner * If we don't have a freeblock in hand, get the next one. 174630f712c9SDave Chinner */ 174730f712c9SDave Chinner if (fbp == NULL) { 174830f712c9SDave Chinner /* 174930f712c9SDave Chinner * Happens the first time through unless lookup gave 175030f712c9SDave Chinner * us a freespace block to start with. 175130f712c9SDave Chinner */ 175230f712c9SDave Chinner if (++fbno == 0) 175330f712c9SDave Chinner fbno = xfs_dir2_byte_to_db(args->geo, 175430f712c9SDave Chinner XFS_DIR2_FREE_OFFSET); 175530f712c9SDave Chinner /* 175630f712c9SDave Chinner * If it's ifbno we already looked at it. 175730f712c9SDave Chinner */ 175830f712c9SDave Chinner if (fbno == ifbno) 175930f712c9SDave Chinner fbno++; 176030f712c9SDave Chinner /* 176130f712c9SDave Chinner * If it's off the end we're done. 176230f712c9SDave Chinner */ 176330f712c9SDave Chinner if (fbno >= lastfbno) 176430f712c9SDave Chinner break; 176530f712c9SDave Chinner /* 176630f712c9SDave Chinner * Read the block. There can be holes in the 176730f712c9SDave Chinner * freespace blocks, so this might not succeed. 176830f712c9SDave Chinner * This should be really rare, so there's no reason 176930f712c9SDave Chinner * to avoid it. 177030f712c9SDave Chinner */ 177130f712c9SDave Chinner error = xfs_dir2_free_try_read(tp, dp, 177230f712c9SDave Chinner xfs_dir2_db_to_da(args->geo, fbno), 177330f712c9SDave Chinner &fbp); 177430f712c9SDave Chinner if (error) 177530f712c9SDave Chinner return error; 177630f712c9SDave Chinner if (!fbp) 177730f712c9SDave Chinner continue; 177830f712c9SDave Chinner free = fbp->b_addr; 177930f712c9SDave Chinner findex = 0; 178030f712c9SDave Chinner } 178130f712c9SDave Chinner /* 178230f712c9SDave Chinner * Look at the current free entry. Is it good enough? 178330f712c9SDave Chinner * 178430f712c9SDave Chinner * The bests initialisation should be where the bufer is read in 178530f712c9SDave Chinner * the above branch. But gcc is too stupid to realise that bests 178630f712c9SDave Chinner * and the freehdr are actually initialised if they are placed 178730f712c9SDave Chinner * there, so we have to do it here to avoid warnings. Blech. 178830f712c9SDave Chinner */ 178930f712c9SDave Chinner bests = dp->d_ops->free_bests_p(free); 179030f712c9SDave Chinner dp->d_ops->free_hdr_from_disk(&freehdr, free); 179130f712c9SDave Chinner if (be16_to_cpu(bests[findex]) != NULLDATAOFF && 179230f712c9SDave Chinner be16_to_cpu(bests[findex]) >= length) 179330f712c9SDave Chinner dbno = freehdr.firstdb + findex; 179430f712c9SDave Chinner else { 179530f712c9SDave Chinner /* 179630f712c9SDave Chinner * Are we done with the freeblock? 179730f712c9SDave Chinner */ 179830f712c9SDave Chinner if (++findex == freehdr.nvalid) { 179930f712c9SDave Chinner /* 180030f712c9SDave Chinner * Drop the block. 180130f712c9SDave Chinner */ 180230f712c9SDave Chinner xfs_trans_brelse(tp, fbp); 180330f712c9SDave Chinner fbp = NULL; 180430f712c9SDave Chinner if (fblk && fblk->bp) 180530f712c9SDave Chinner fblk->bp = NULL; 180630f712c9SDave Chinner } 180730f712c9SDave Chinner } 180830f712c9SDave Chinner } 180930f712c9SDave Chinner /* 181030f712c9SDave Chinner * If we don't have a data block, we need to allocate one and make 181130f712c9SDave Chinner * the freespace entries refer to it. 181230f712c9SDave Chinner */ 181330f712c9SDave Chinner if (unlikely(dbno == -1)) { 181430f712c9SDave Chinner /* 181530f712c9SDave Chinner * Not allowed to allocate, return failure. 181630f712c9SDave Chinner */ 181730f712c9SDave Chinner if ((args->op_flags & XFS_DA_OP_JUSTCHECK) || args->total == 0) 1818*2451337dSDave Chinner return -ENOSPC; 181930f712c9SDave Chinner 182030f712c9SDave Chinner /* 182130f712c9SDave Chinner * Allocate and initialize the new data block. 182230f712c9SDave Chinner */ 182330f712c9SDave Chinner if (unlikely((error = xfs_dir2_grow_inode(args, 182430f712c9SDave Chinner XFS_DIR2_DATA_SPACE, 182530f712c9SDave Chinner &dbno)) || 182630f712c9SDave Chinner (error = xfs_dir3_data_init(args, dbno, &dbp)))) 182730f712c9SDave Chinner return error; 182830f712c9SDave Chinner 182930f712c9SDave Chinner /* 183030f712c9SDave Chinner * If (somehow) we have a freespace block, get rid of it. 183130f712c9SDave Chinner */ 183230f712c9SDave Chinner if (fbp) 183330f712c9SDave Chinner xfs_trans_brelse(tp, fbp); 183430f712c9SDave Chinner if (fblk && fblk->bp) 183530f712c9SDave Chinner fblk->bp = NULL; 183630f712c9SDave Chinner 183730f712c9SDave Chinner /* 183830f712c9SDave Chinner * Get the freespace block corresponding to the data block 183930f712c9SDave Chinner * that was just allocated. 184030f712c9SDave Chinner */ 184130f712c9SDave Chinner fbno = dp->d_ops->db_to_fdb(args->geo, dbno); 184230f712c9SDave Chinner error = xfs_dir2_free_try_read(tp, dp, 184330f712c9SDave Chinner xfs_dir2_db_to_da(args->geo, fbno), 184430f712c9SDave Chinner &fbp); 184530f712c9SDave Chinner if (error) 184630f712c9SDave Chinner return error; 184730f712c9SDave Chinner 184830f712c9SDave Chinner /* 184930f712c9SDave Chinner * If there wasn't a freespace block, the read will 185030f712c9SDave Chinner * return a NULL fbp. Allocate and initialize a new one. 185130f712c9SDave Chinner */ 185230f712c9SDave Chinner if (!fbp) { 185330f712c9SDave Chinner error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, 185430f712c9SDave Chinner &fbno); 185530f712c9SDave Chinner if (error) 185630f712c9SDave Chinner return error; 185730f712c9SDave Chinner 185830f712c9SDave Chinner if (dp->d_ops->db_to_fdb(args->geo, dbno) != fbno) { 185930f712c9SDave Chinner xfs_alert(mp, 186030f712c9SDave Chinner "%s: dir ino %llu needed freesp block %lld for\n" 186130f712c9SDave Chinner " data block %lld, got %lld ifbno %llu lastfbno %d", 186230f712c9SDave Chinner __func__, (unsigned long long)dp->i_ino, 186330f712c9SDave Chinner (long long)dp->d_ops->db_to_fdb( 186430f712c9SDave Chinner args->geo, dbno), 186530f712c9SDave Chinner (long long)dbno, (long long)fbno, 186630f712c9SDave Chinner (unsigned long long)ifbno, lastfbno); 186730f712c9SDave Chinner if (fblk) { 186830f712c9SDave Chinner xfs_alert(mp, 186930f712c9SDave Chinner " fblk 0x%p blkno %llu index %d magic 0x%x", 187030f712c9SDave Chinner fblk, 187130f712c9SDave Chinner (unsigned long long)fblk->blkno, 187230f712c9SDave Chinner fblk->index, 187330f712c9SDave Chinner fblk->magic); 187430f712c9SDave Chinner } else { 187530f712c9SDave Chinner xfs_alert(mp, " ... fblk is NULL"); 187630f712c9SDave Chinner } 187730f712c9SDave Chinner XFS_ERROR_REPORT("xfs_dir2_node_addname_int", 187830f712c9SDave Chinner XFS_ERRLEVEL_LOW, mp); 1879*2451337dSDave Chinner return -EFSCORRUPTED; 188030f712c9SDave Chinner } 188130f712c9SDave Chinner 188230f712c9SDave Chinner /* 188330f712c9SDave Chinner * Get a buffer for the new block. 188430f712c9SDave Chinner */ 188530f712c9SDave Chinner error = xfs_dir3_free_get_buf(args, fbno, &fbp); 188630f712c9SDave Chinner if (error) 188730f712c9SDave Chinner return error; 188830f712c9SDave Chinner free = fbp->b_addr; 188930f712c9SDave Chinner bests = dp->d_ops->free_bests_p(free); 189030f712c9SDave Chinner dp->d_ops->free_hdr_from_disk(&freehdr, free); 189130f712c9SDave Chinner 189230f712c9SDave Chinner /* 189330f712c9SDave Chinner * Remember the first slot as our empty slot. 189430f712c9SDave Chinner */ 189530f712c9SDave Chinner freehdr.firstdb = 189630f712c9SDave Chinner (fbno - xfs_dir2_byte_to_db(args->geo, 189730f712c9SDave Chinner XFS_DIR2_FREE_OFFSET)) * 189830f712c9SDave Chinner dp->d_ops->free_max_bests(args->geo); 189930f712c9SDave Chinner } else { 190030f712c9SDave Chinner free = fbp->b_addr; 190130f712c9SDave Chinner bests = dp->d_ops->free_bests_p(free); 190230f712c9SDave Chinner dp->d_ops->free_hdr_from_disk(&freehdr, free); 190330f712c9SDave Chinner } 190430f712c9SDave Chinner 190530f712c9SDave Chinner /* 190630f712c9SDave Chinner * Set the freespace block index from the data block number. 190730f712c9SDave Chinner */ 190830f712c9SDave Chinner findex = dp->d_ops->db_to_fdindex(args->geo, dbno); 190930f712c9SDave Chinner /* 191030f712c9SDave Chinner * If it's after the end of the current entries in the 191130f712c9SDave Chinner * freespace block, extend that table. 191230f712c9SDave Chinner */ 191330f712c9SDave Chinner if (findex >= freehdr.nvalid) { 191430f712c9SDave Chinner ASSERT(findex < dp->d_ops->free_max_bests(args->geo)); 191530f712c9SDave Chinner freehdr.nvalid = findex + 1; 191630f712c9SDave Chinner /* 191730f712c9SDave Chinner * Tag new entry so nused will go up. 191830f712c9SDave Chinner */ 191930f712c9SDave Chinner bests[findex] = cpu_to_be16(NULLDATAOFF); 192030f712c9SDave Chinner } 192130f712c9SDave Chinner /* 192230f712c9SDave Chinner * If this entry was for an empty data block 192330f712c9SDave Chinner * (this should always be true) then update the header. 192430f712c9SDave Chinner */ 192530f712c9SDave Chinner if (bests[findex] == cpu_to_be16(NULLDATAOFF)) { 192630f712c9SDave Chinner freehdr.nused++; 192730f712c9SDave Chinner dp->d_ops->free_hdr_to_disk(fbp->b_addr, &freehdr); 192830f712c9SDave Chinner xfs_dir2_free_log_header(args, fbp); 192930f712c9SDave Chinner } 193030f712c9SDave Chinner /* 193130f712c9SDave Chinner * Update the real value in the table. 193230f712c9SDave Chinner * We haven't allocated the data entry yet so this will 193330f712c9SDave Chinner * change again. 193430f712c9SDave Chinner */ 193530f712c9SDave Chinner hdr = dbp->b_addr; 193630f712c9SDave Chinner bf = dp->d_ops->data_bestfree_p(hdr); 193730f712c9SDave Chinner bests[findex] = bf[0].length; 193830f712c9SDave Chinner logfree = 1; 193930f712c9SDave Chinner } 194030f712c9SDave Chinner /* 194130f712c9SDave Chinner * We had a data block so we don't have to make a new one. 194230f712c9SDave Chinner */ 194330f712c9SDave Chinner else { 194430f712c9SDave Chinner /* 194530f712c9SDave Chinner * If just checking, we succeeded. 194630f712c9SDave Chinner */ 194730f712c9SDave Chinner if (args->op_flags & XFS_DA_OP_JUSTCHECK) 194830f712c9SDave Chinner return 0; 194930f712c9SDave Chinner 195030f712c9SDave Chinner /* 195130f712c9SDave Chinner * Read the data block in. 195230f712c9SDave Chinner */ 195330f712c9SDave Chinner error = xfs_dir3_data_read(tp, dp, 195430f712c9SDave Chinner xfs_dir2_db_to_da(args->geo, dbno), 195530f712c9SDave Chinner -1, &dbp); 195630f712c9SDave Chinner if (error) 195730f712c9SDave Chinner return error; 195830f712c9SDave Chinner hdr = dbp->b_addr; 195930f712c9SDave Chinner bf = dp->d_ops->data_bestfree_p(hdr); 196030f712c9SDave Chinner logfree = 0; 196130f712c9SDave Chinner } 196230f712c9SDave Chinner ASSERT(be16_to_cpu(bf[0].length) >= length); 196330f712c9SDave Chinner /* 196430f712c9SDave Chinner * Point to the existing unused space. 196530f712c9SDave Chinner */ 196630f712c9SDave Chinner dup = (xfs_dir2_data_unused_t *) 196730f712c9SDave Chinner ((char *)hdr + be16_to_cpu(bf[0].offset)); 196830f712c9SDave Chinner needscan = needlog = 0; 196930f712c9SDave Chinner /* 197030f712c9SDave Chinner * Mark the first part of the unused space, inuse for us. 197130f712c9SDave Chinner */ 197230f712c9SDave Chinner xfs_dir2_data_use_free(args, dbp, dup, 197330f712c9SDave Chinner (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr), length, 197430f712c9SDave Chinner &needlog, &needscan); 197530f712c9SDave Chinner /* 197630f712c9SDave Chinner * Fill in the new entry and log it. 197730f712c9SDave Chinner */ 197830f712c9SDave Chinner dep = (xfs_dir2_data_entry_t *)dup; 197930f712c9SDave Chinner dep->inumber = cpu_to_be64(args->inumber); 198030f712c9SDave Chinner dep->namelen = args->namelen; 198130f712c9SDave Chinner memcpy(dep->name, args->name, dep->namelen); 198230f712c9SDave Chinner dp->d_ops->data_put_ftype(dep, args->filetype); 198330f712c9SDave Chinner tagp = dp->d_ops->data_entry_tag_p(dep); 198430f712c9SDave Chinner *tagp = cpu_to_be16((char *)dep - (char *)hdr); 198530f712c9SDave Chinner xfs_dir2_data_log_entry(args, dbp, dep); 198630f712c9SDave Chinner /* 198730f712c9SDave Chinner * Rescan the block for bestfree if needed. 198830f712c9SDave Chinner */ 198930f712c9SDave Chinner if (needscan) 199030f712c9SDave Chinner xfs_dir2_data_freescan(dp, hdr, &needlog); 199130f712c9SDave Chinner /* 199230f712c9SDave Chinner * Log the data block header if needed. 199330f712c9SDave Chinner */ 199430f712c9SDave Chinner if (needlog) 199530f712c9SDave Chinner xfs_dir2_data_log_header(args, dbp); 199630f712c9SDave Chinner /* 199730f712c9SDave Chinner * If the freespace entry is now wrong, update it. 199830f712c9SDave Chinner */ 199930f712c9SDave Chinner bests = dp->d_ops->free_bests_p(free); /* gcc is so stupid */ 200030f712c9SDave Chinner if (be16_to_cpu(bests[findex]) != be16_to_cpu(bf[0].length)) { 200130f712c9SDave Chinner bests[findex] = bf[0].length; 200230f712c9SDave Chinner logfree = 1; 200330f712c9SDave Chinner } 200430f712c9SDave Chinner /* 200530f712c9SDave Chinner * Log the freespace entry if needed. 200630f712c9SDave Chinner */ 200730f712c9SDave Chinner if (logfree) 200830f712c9SDave Chinner xfs_dir2_free_log_bests(args, fbp, findex, findex); 200930f712c9SDave Chinner /* 201030f712c9SDave Chinner * Return the data block and offset in args, then drop the data block. 201130f712c9SDave Chinner */ 201230f712c9SDave Chinner args->blkno = (xfs_dablk_t)dbno; 201330f712c9SDave Chinner args->index = be16_to_cpu(*tagp); 201430f712c9SDave Chinner return 0; 201530f712c9SDave Chinner } 201630f712c9SDave Chinner 201730f712c9SDave Chinner /* 201830f712c9SDave Chinner * Lookup an entry in a node-format directory. 201930f712c9SDave Chinner * All the real work happens in xfs_da3_node_lookup_int. 202030f712c9SDave Chinner * The only real output is the inode number of the entry. 202130f712c9SDave Chinner */ 202230f712c9SDave Chinner int /* error */ 202330f712c9SDave Chinner xfs_dir2_node_lookup( 202430f712c9SDave Chinner xfs_da_args_t *args) /* operation arguments */ 202530f712c9SDave Chinner { 202630f712c9SDave Chinner int error; /* error return value */ 202730f712c9SDave Chinner int i; /* btree level */ 202830f712c9SDave Chinner int rval; /* operation return value */ 202930f712c9SDave Chinner xfs_da_state_t *state; /* btree cursor */ 203030f712c9SDave Chinner 203130f712c9SDave Chinner trace_xfs_dir2_node_lookup(args); 203230f712c9SDave Chinner 203330f712c9SDave Chinner /* 203430f712c9SDave Chinner * Allocate and initialize the btree cursor. 203530f712c9SDave Chinner */ 203630f712c9SDave Chinner state = xfs_da_state_alloc(); 203730f712c9SDave Chinner state->args = args; 203830f712c9SDave Chinner state->mp = args->dp->i_mount; 203930f712c9SDave Chinner /* 204030f712c9SDave Chinner * Fill in the path to the entry in the cursor. 204130f712c9SDave Chinner */ 204230f712c9SDave Chinner error = xfs_da3_node_lookup_int(state, &rval); 204330f712c9SDave Chinner if (error) 204430f712c9SDave Chinner rval = error; 2045*2451337dSDave Chinner else if (rval == -ENOENT && args->cmpresult == XFS_CMP_CASE) { 2046*2451337dSDave Chinner /* If a CI match, dup the actual name and return -EEXIST */ 204730f712c9SDave Chinner xfs_dir2_data_entry_t *dep; 204830f712c9SDave Chinner 204930f712c9SDave Chinner dep = (xfs_dir2_data_entry_t *) 205030f712c9SDave Chinner ((char *)state->extrablk.bp->b_addr + 205130f712c9SDave Chinner state->extrablk.index); 205230f712c9SDave Chinner rval = xfs_dir_cilookup_result(args, dep->name, dep->namelen); 205330f712c9SDave Chinner } 205430f712c9SDave Chinner /* 205530f712c9SDave Chinner * Release the btree blocks and leaf block. 205630f712c9SDave Chinner */ 205730f712c9SDave Chinner for (i = 0; i < state->path.active; i++) { 205830f712c9SDave Chinner xfs_trans_brelse(args->trans, state->path.blk[i].bp); 205930f712c9SDave Chinner state->path.blk[i].bp = NULL; 206030f712c9SDave Chinner } 206130f712c9SDave Chinner /* 206230f712c9SDave Chinner * Release the data block if we have it. 206330f712c9SDave Chinner */ 206430f712c9SDave Chinner if (state->extravalid && state->extrablk.bp) { 206530f712c9SDave Chinner xfs_trans_brelse(args->trans, state->extrablk.bp); 206630f712c9SDave Chinner state->extrablk.bp = NULL; 206730f712c9SDave Chinner } 206830f712c9SDave Chinner xfs_da_state_free(state); 206930f712c9SDave Chinner return rval; 207030f712c9SDave Chinner } 207130f712c9SDave Chinner 207230f712c9SDave Chinner /* 207330f712c9SDave Chinner * Remove an entry from a node-format directory. 207430f712c9SDave Chinner */ 207530f712c9SDave Chinner int /* error */ 207630f712c9SDave Chinner xfs_dir2_node_removename( 207730f712c9SDave Chinner struct xfs_da_args *args) /* operation arguments */ 207830f712c9SDave Chinner { 207930f712c9SDave Chinner struct xfs_da_state_blk *blk; /* leaf block */ 208030f712c9SDave Chinner int error; /* error return value */ 208130f712c9SDave Chinner int rval; /* operation return value */ 208230f712c9SDave Chinner struct xfs_da_state *state; /* btree cursor */ 208330f712c9SDave Chinner 208430f712c9SDave Chinner trace_xfs_dir2_node_removename(args); 208530f712c9SDave Chinner 208630f712c9SDave Chinner /* 208730f712c9SDave Chinner * Allocate and initialize the btree cursor. 208830f712c9SDave Chinner */ 208930f712c9SDave Chinner state = xfs_da_state_alloc(); 209030f712c9SDave Chinner state->args = args; 209130f712c9SDave Chinner state->mp = args->dp->i_mount; 209230f712c9SDave Chinner 209330f712c9SDave Chinner /* Look up the entry we're deleting, set up the cursor. */ 209430f712c9SDave Chinner error = xfs_da3_node_lookup_int(state, &rval); 209530f712c9SDave Chinner if (error) 209630f712c9SDave Chinner goto out_free; 209730f712c9SDave Chinner 209830f712c9SDave Chinner /* Didn't find it, upper layer screwed up. */ 2099*2451337dSDave Chinner if (rval != -EEXIST) { 210030f712c9SDave Chinner error = rval; 210130f712c9SDave Chinner goto out_free; 210230f712c9SDave Chinner } 210330f712c9SDave Chinner 210430f712c9SDave Chinner blk = &state->path.blk[state->path.active - 1]; 210530f712c9SDave Chinner ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC); 210630f712c9SDave Chinner ASSERT(state->extravalid); 210730f712c9SDave Chinner /* 210830f712c9SDave Chinner * Remove the leaf and data entries. 210930f712c9SDave Chinner * Extrablk refers to the data block. 211030f712c9SDave Chinner */ 211130f712c9SDave Chinner error = xfs_dir2_leafn_remove(args, blk->bp, blk->index, 211230f712c9SDave Chinner &state->extrablk, &rval); 211330f712c9SDave Chinner if (error) 211430f712c9SDave Chinner goto out_free; 211530f712c9SDave Chinner /* 211630f712c9SDave Chinner * Fix the hash values up the btree. 211730f712c9SDave Chinner */ 211830f712c9SDave Chinner xfs_da3_fixhashpath(state, &state->path); 211930f712c9SDave Chinner /* 212030f712c9SDave Chinner * If we need to join leaf blocks, do it. 212130f712c9SDave Chinner */ 212230f712c9SDave Chinner if (rval && state->path.active > 1) 212330f712c9SDave Chinner error = xfs_da3_join(state); 212430f712c9SDave Chinner /* 212530f712c9SDave Chinner * If no errors so far, try conversion to leaf format. 212630f712c9SDave Chinner */ 212730f712c9SDave Chinner if (!error) 212830f712c9SDave Chinner error = xfs_dir2_node_to_leaf(state); 212930f712c9SDave Chinner out_free: 213030f712c9SDave Chinner xfs_da_state_free(state); 213130f712c9SDave Chinner return error; 213230f712c9SDave Chinner } 213330f712c9SDave Chinner 213430f712c9SDave Chinner /* 213530f712c9SDave Chinner * Replace an entry's inode number in a node-format directory. 213630f712c9SDave Chinner */ 213730f712c9SDave Chinner int /* error */ 213830f712c9SDave Chinner xfs_dir2_node_replace( 213930f712c9SDave Chinner xfs_da_args_t *args) /* operation arguments */ 214030f712c9SDave Chinner { 214130f712c9SDave Chinner xfs_da_state_blk_t *blk; /* leaf block */ 214230f712c9SDave Chinner xfs_dir2_data_hdr_t *hdr; /* data block header */ 214330f712c9SDave Chinner xfs_dir2_data_entry_t *dep; /* data entry changed */ 214430f712c9SDave Chinner int error; /* error return value */ 214530f712c9SDave Chinner int i; /* btree level */ 214630f712c9SDave Chinner xfs_ino_t inum; /* new inode number */ 214730f712c9SDave Chinner xfs_dir2_leaf_t *leaf; /* leaf structure */ 214830f712c9SDave Chinner xfs_dir2_leaf_entry_t *lep; /* leaf entry being changed */ 214930f712c9SDave Chinner int rval; /* internal return value */ 215030f712c9SDave Chinner xfs_da_state_t *state; /* btree cursor */ 215130f712c9SDave Chinner 215230f712c9SDave Chinner trace_xfs_dir2_node_replace(args); 215330f712c9SDave Chinner 215430f712c9SDave Chinner /* 215530f712c9SDave Chinner * Allocate and initialize the btree cursor. 215630f712c9SDave Chinner */ 215730f712c9SDave Chinner state = xfs_da_state_alloc(); 215830f712c9SDave Chinner state->args = args; 215930f712c9SDave Chinner state->mp = args->dp->i_mount; 216030f712c9SDave Chinner inum = args->inumber; 216130f712c9SDave Chinner /* 216230f712c9SDave Chinner * Lookup the entry to change in the btree. 216330f712c9SDave Chinner */ 216430f712c9SDave Chinner error = xfs_da3_node_lookup_int(state, &rval); 216530f712c9SDave Chinner if (error) { 216630f712c9SDave Chinner rval = error; 216730f712c9SDave Chinner } 216830f712c9SDave Chinner /* 216930f712c9SDave Chinner * It should be found, since the vnodeops layer has looked it up 217030f712c9SDave Chinner * and locked it. But paranoia is good. 217130f712c9SDave Chinner */ 2172*2451337dSDave Chinner if (rval == -EEXIST) { 217330f712c9SDave Chinner struct xfs_dir2_leaf_entry *ents; 217430f712c9SDave Chinner /* 217530f712c9SDave Chinner * Find the leaf entry. 217630f712c9SDave Chinner */ 217730f712c9SDave Chinner blk = &state->path.blk[state->path.active - 1]; 217830f712c9SDave Chinner ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC); 217930f712c9SDave Chinner leaf = blk->bp->b_addr; 218030f712c9SDave Chinner ents = args->dp->d_ops->leaf_ents_p(leaf); 218130f712c9SDave Chinner lep = &ents[blk->index]; 218230f712c9SDave Chinner ASSERT(state->extravalid); 218330f712c9SDave Chinner /* 218430f712c9SDave Chinner * Point to the data entry. 218530f712c9SDave Chinner */ 218630f712c9SDave Chinner hdr = state->extrablk.bp->b_addr; 218730f712c9SDave Chinner ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) || 218830f712c9SDave Chinner hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC)); 218930f712c9SDave Chinner dep = (xfs_dir2_data_entry_t *) 219030f712c9SDave Chinner ((char *)hdr + 219130f712c9SDave Chinner xfs_dir2_dataptr_to_off(args->geo, 219230f712c9SDave Chinner be32_to_cpu(lep->address))); 219330f712c9SDave Chinner ASSERT(inum != be64_to_cpu(dep->inumber)); 219430f712c9SDave Chinner /* 219530f712c9SDave Chinner * Fill in the new inode number and log the entry. 219630f712c9SDave Chinner */ 219730f712c9SDave Chinner dep->inumber = cpu_to_be64(inum); 219830f712c9SDave Chinner args->dp->d_ops->data_put_ftype(dep, args->filetype); 219930f712c9SDave Chinner xfs_dir2_data_log_entry(args, state->extrablk.bp, dep); 220030f712c9SDave Chinner rval = 0; 220130f712c9SDave Chinner } 220230f712c9SDave Chinner /* 220330f712c9SDave Chinner * Didn't find it, and we're holding a data block. Drop it. 220430f712c9SDave Chinner */ 220530f712c9SDave Chinner else if (state->extravalid) { 220630f712c9SDave Chinner xfs_trans_brelse(args->trans, state->extrablk.bp); 220730f712c9SDave Chinner state->extrablk.bp = NULL; 220830f712c9SDave Chinner } 220930f712c9SDave Chinner /* 221030f712c9SDave Chinner * Release all the buffers in the cursor. 221130f712c9SDave Chinner */ 221230f712c9SDave Chinner for (i = 0; i < state->path.active; i++) { 221330f712c9SDave Chinner xfs_trans_brelse(args->trans, state->path.blk[i].bp); 221430f712c9SDave Chinner state->path.blk[i].bp = NULL; 221530f712c9SDave Chinner } 221630f712c9SDave Chinner xfs_da_state_free(state); 221730f712c9SDave Chinner return rval; 221830f712c9SDave Chinner } 221930f712c9SDave Chinner 222030f712c9SDave Chinner /* 222130f712c9SDave Chinner * Trim off a trailing empty freespace block. 222230f712c9SDave Chinner * Return (in rvalp) 1 if we did it, 0 if not. 222330f712c9SDave Chinner */ 222430f712c9SDave Chinner int /* error */ 222530f712c9SDave Chinner xfs_dir2_node_trim_free( 222630f712c9SDave Chinner xfs_da_args_t *args, /* operation arguments */ 222730f712c9SDave Chinner xfs_fileoff_t fo, /* free block number */ 222830f712c9SDave Chinner int *rvalp) /* out: did something */ 222930f712c9SDave Chinner { 223030f712c9SDave Chinner struct xfs_buf *bp; /* freespace buffer */ 223130f712c9SDave Chinner xfs_inode_t *dp; /* incore directory inode */ 223230f712c9SDave Chinner int error; /* error return code */ 223330f712c9SDave Chinner xfs_dir2_free_t *free; /* freespace structure */ 223430f712c9SDave Chinner xfs_mount_t *mp; /* filesystem mount point */ 223530f712c9SDave Chinner xfs_trans_t *tp; /* transaction pointer */ 223630f712c9SDave Chinner struct xfs_dir3_icfree_hdr freehdr; 223730f712c9SDave Chinner 223830f712c9SDave Chinner dp = args->dp; 223930f712c9SDave Chinner mp = dp->i_mount; 224030f712c9SDave Chinner tp = args->trans; 224130f712c9SDave Chinner /* 224230f712c9SDave Chinner * Read the freespace block. 224330f712c9SDave Chinner */ 224430f712c9SDave Chinner error = xfs_dir2_free_try_read(tp, dp, fo, &bp); 224530f712c9SDave Chinner if (error) 224630f712c9SDave Chinner return error; 224730f712c9SDave Chinner /* 224830f712c9SDave Chinner * There can be holes in freespace. If fo is a hole, there's 224930f712c9SDave Chinner * nothing to do. 225030f712c9SDave Chinner */ 225130f712c9SDave Chinner if (!bp) 225230f712c9SDave Chinner return 0; 225330f712c9SDave Chinner free = bp->b_addr; 225430f712c9SDave Chinner dp->d_ops->free_hdr_from_disk(&freehdr, free); 225530f712c9SDave Chinner 225630f712c9SDave Chinner /* 225730f712c9SDave Chinner * If there are used entries, there's nothing to do. 225830f712c9SDave Chinner */ 225930f712c9SDave Chinner if (freehdr.nused > 0) { 226030f712c9SDave Chinner xfs_trans_brelse(tp, bp); 226130f712c9SDave Chinner *rvalp = 0; 226230f712c9SDave Chinner return 0; 226330f712c9SDave Chinner } 226430f712c9SDave Chinner /* 226530f712c9SDave Chinner * Blow the block away. 226630f712c9SDave Chinner */ 226730f712c9SDave Chinner error = xfs_dir2_shrink_inode(args, 226830f712c9SDave Chinner xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)fo), bp); 226930f712c9SDave Chinner if (error) { 227030f712c9SDave Chinner /* 227130f712c9SDave Chinner * Can't fail with ENOSPC since that only happens with no 227230f712c9SDave Chinner * space reservation, when breaking up an extent into two 227330f712c9SDave Chinner * pieces. This is the last block of an extent. 227430f712c9SDave Chinner */ 2275*2451337dSDave Chinner ASSERT(error != -ENOSPC); 227630f712c9SDave Chinner xfs_trans_brelse(tp, bp); 227730f712c9SDave Chinner return error; 227830f712c9SDave Chinner } 227930f712c9SDave Chinner /* 228030f712c9SDave Chinner * Return that we succeeded. 228130f712c9SDave Chinner */ 228230f712c9SDave Chinner *rvalp = 1; 228330f712c9SDave Chinner return 0; 228430f712c9SDave Chinner } 2285