Dave Chinner | 0b61f8a | 2018-06-05 19:42:14 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 3 | * Copyright (c) 2000-2005 Silicon Graphics, Inc. |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 4 | * Copyright (c) 2013 Red Hat, Inc. |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 5 | * All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | #include "xfs.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 8 | #include "xfs_fs.h" |
Darrick J. Wong | 5467b34 | 2019-06-28 19:25:35 -0700 | [diff] [blame^] | 9 | #include "xfs_shared.h" |
Dave Chinner | a4fbe6a | 2013-10-23 10:51:50 +1100 | [diff] [blame] | 10 | #include "xfs_format.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 11 | #include "xfs_log_format.h" |
| 12 | #include "xfs_trans_resv.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include "xfs_mount.h" |
Dave Chinner | 5706278 | 2013-10-15 09:17:51 +1100 | [diff] [blame] | 14 | #include "xfs_da_format.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 15 | #include "xfs_da_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include "xfs_inode.h" |
| 17 | #include "xfs_bmap.h" |
Dave Chinner | 2b9ab5a | 2013-08-12 20:49:37 +1000 | [diff] [blame] | 18 | #include "xfs_dir2.h" |
Christoph Hellwig | 5792664 | 2011-07-13 13:43:48 +0200 | [diff] [blame] | 19 | #include "xfs_dir2_priv.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include "xfs_error.h" |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 21 | #include "xfs_trace.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 22 | #include "xfs_trans.h" |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 23 | #include "xfs_buf_item.h" |
| 24 | #include "xfs_cksum.h" |
Brian Foster | a45086e | 2015-10-12 15:59:25 +1100 | [diff] [blame] | 25 | #include "xfs_log.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
| 27 | /* |
| 28 | * Function declarations. |
| 29 | */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 30 | static int xfs_dir2_leafn_add(struct xfs_buf *bp, xfs_da_args_t *args, |
| 31 | int index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | static void xfs_dir2_leafn_rebalance(xfs_da_state_t *state, |
| 33 | xfs_da_state_blk_t *blk1, |
| 34 | xfs_da_state_blk_t *blk2); |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 35 | static int xfs_dir2_leafn_remove(xfs_da_args_t *args, struct xfs_buf *bp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | int index, xfs_da_state_blk_t *dblk, |
| 37 | int *rval); |
| 38 | static int xfs_dir2_node_addname_int(xfs_da_args_t *args, |
| 39 | xfs_da_state_blk_t *fblk); |
| 40 | |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 41 | /* |
| 42 | * Check internal consistency of a leafn block. |
| 43 | */ |
| 44 | #ifdef DEBUG |
Darrick J. Wong | a6a781a | 2018-01-08 10:51:03 -0800 | [diff] [blame] | 45 | static xfs_failaddr_t |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 46 | xfs_dir3_leafn_check( |
Dave Chinner | 4141956 | 2013-10-29 22:11:50 +1100 | [diff] [blame] | 47 | struct xfs_inode *dp, |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 48 | struct xfs_buf *bp) |
| 49 | { |
| 50 | struct xfs_dir2_leaf *leaf = bp->b_addr; |
| 51 | struct xfs_dir3_icleaf_hdr leafhdr; |
| 52 | |
Dave Chinner | 01ba43b | 2013-10-29 22:11:52 +1100 | [diff] [blame] | 53 | dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf); |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 54 | |
| 55 | if (leafhdr.magic == XFS_DIR3_LEAFN_MAGIC) { |
| 56 | struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr; |
| 57 | if (be64_to_cpu(leaf3->info.blkno) != bp->b_bn) |
Darrick J. Wong | a6a781a | 2018-01-08 10:51:03 -0800 | [diff] [blame] | 58 | return __this_address; |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 59 | } else if (leafhdr.magic != XFS_DIR2_LEAFN_MAGIC) |
Darrick J. Wong | a6a781a | 2018-01-08 10:51:03 -0800 | [diff] [blame] | 60 | return __this_address; |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 61 | |
Dave Chinner | 4141956 | 2013-10-29 22:11:50 +1100 | [diff] [blame] | 62 | return xfs_dir3_leaf_check_int(dp->i_mount, dp, &leafhdr, leaf); |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 63 | } |
Darrick J. Wong | a6a781a | 2018-01-08 10:51:03 -0800 | [diff] [blame] | 64 | |
| 65 | static inline void |
| 66 | xfs_dir3_leaf_check( |
| 67 | struct xfs_inode *dp, |
| 68 | struct xfs_buf *bp) |
| 69 | { |
| 70 | xfs_failaddr_t fa; |
| 71 | |
| 72 | fa = xfs_dir3_leafn_check(dp, bp); |
| 73 | if (!fa) |
| 74 | return; |
| 75 | xfs_corruption_error(__func__, XFS_ERRLEVEL_LOW, dp->i_mount, |
Darrick J. Wong | 2551a53 | 2018-06-04 10:23:54 -0700 | [diff] [blame] | 76 | bp->b_addr, BBTOB(bp->b_length), __FILE__, __LINE__, |
| 77 | fa); |
Darrick J. Wong | a6a781a | 2018-01-08 10:51:03 -0800 | [diff] [blame] | 78 | ASSERT(0); |
| 79 | } |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 80 | #else |
Dave Chinner | 4141956 | 2013-10-29 22:11:50 +1100 | [diff] [blame] | 81 | #define xfs_dir3_leaf_check(dp, bp) |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 82 | #endif |
| 83 | |
Darrick J. Wong | a6a781a | 2018-01-08 10:51:03 -0800 | [diff] [blame] | 84 | static xfs_failaddr_t |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 85 | xfs_dir3_free_verify( |
Dave Chinner | 2025207 | 2012-11-12 22:54:13 +1100 | [diff] [blame] | 86 | struct xfs_buf *bp) |
| 87 | { |
| 88 | struct xfs_mount *mp = bp->b_target->bt_mount; |
| 89 | struct xfs_dir2_free_hdr *hdr = bp->b_addr; |
Dave Chinner | 2025207 | 2012-11-12 22:54:13 +1100 | [diff] [blame] | 90 | |
Brian Foster | 39708c2 | 2019-02-07 10:45:48 -0800 | [diff] [blame] | 91 | if (!xfs_verify_magic(bp, hdr->magic)) |
| 92 | return __this_address; |
| 93 | |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 94 | if (xfs_sb_version_hascrc(&mp->m_sb)) { |
| 95 | struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr; |
| 96 | |
Eric Sandeen | ce748ea | 2015-07-29 11:53:31 +1000 | [diff] [blame] | 97 | if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_meta_uuid)) |
Darrick J. Wong | a6a781a | 2018-01-08 10:51:03 -0800 | [diff] [blame] | 98 | return __this_address; |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 99 | if (be64_to_cpu(hdr3->blkno) != bp->b_bn) |
Darrick J. Wong | a6a781a | 2018-01-08 10:51:03 -0800 | [diff] [blame] | 100 | return __this_address; |
Brian Foster | a45086e | 2015-10-12 15:59:25 +1100 | [diff] [blame] | 101 | if (!xfs_log_check_lsn(mp, be64_to_cpu(hdr3->lsn))) |
Darrick J. Wong | a6a781a | 2018-01-08 10:51:03 -0800 | [diff] [blame] | 102 | return __this_address; |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /* XXX: should bounds check the xfs_dir3_icfree_hdr here */ |
| 106 | |
Darrick J. Wong | a6a781a | 2018-01-08 10:51:03 -0800 | [diff] [blame] | 107 | return NULL; |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | static void |
| 111 | xfs_dir3_free_read_verify( |
| 112 | struct xfs_buf *bp) |
| 113 | { |
| 114 | struct xfs_mount *mp = bp->b_target->bt_mount; |
Darrick J. Wong | bc1a09b | 2018-01-08 10:51:03 -0800 | [diff] [blame] | 115 | xfs_failaddr_t fa; |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 116 | |
Eric Sandeen | ce5028c | 2014-02-27 15:23:10 +1100 | [diff] [blame] | 117 | if (xfs_sb_version_hascrc(&mp->m_sb) && |
| 118 | !xfs_buf_verify_cksum(bp, XFS_DIR3_FREE_CRC_OFF)) |
Darrick J. Wong | bc1a09b | 2018-01-08 10:51:03 -0800 | [diff] [blame] | 119 | xfs_verifier_error(bp, -EFSBADCRC, __this_address); |
| 120 | else { |
| 121 | fa = xfs_dir3_free_verify(bp); |
| 122 | if (fa) |
| 123 | xfs_verifier_error(bp, -EFSCORRUPTED, fa); |
| 124 | } |
Dave Chinner | 612cfbf | 2012-11-14 17:52:32 +1100 | [diff] [blame] | 125 | } |
Dave Chinner | 2025207 | 2012-11-12 22:54:13 +1100 | [diff] [blame] | 126 | |
Dave Chinner | 612cfbf | 2012-11-14 17:52:32 +1100 | [diff] [blame] | 127 | static void |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 128 | xfs_dir3_free_write_verify( |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 129 | struct xfs_buf *bp) |
| 130 | { |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 131 | struct xfs_mount *mp = bp->b_target->bt_mount; |
Carlos Maiolino | fb1755a | 2018-01-24 13:38:48 -0800 | [diff] [blame] | 132 | struct xfs_buf_log_item *bip = bp->b_log_item; |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 133 | struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr; |
Darrick J. Wong | bc1a09b | 2018-01-08 10:51:03 -0800 | [diff] [blame] | 134 | xfs_failaddr_t fa; |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 135 | |
Darrick J. Wong | bc1a09b | 2018-01-08 10:51:03 -0800 | [diff] [blame] | 136 | fa = xfs_dir3_free_verify(bp); |
| 137 | if (fa) { |
| 138 | xfs_verifier_error(bp, -EFSCORRUPTED, fa); |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 139 | return; |
| 140 | } |
| 141 | |
| 142 | if (!xfs_sb_version_hascrc(&mp->m_sb)) |
| 143 | return; |
| 144 | |
| 145 | if (bip) |
| 146 | hdr3->lsn = cpu_to_be64(bip->bli_item.li_lsn); |
| 147 | |
Eric Sandeen | f1dbcd7 | 2014-02-27 15:18:23 +1100 | [diff] [blame] | 148 | xfs_buf_update_cksum(bp, XFS_DIR3_FREE_CRC_OFF); |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 149 | } |
| 150 | |
Dave Chinner | d75afeb | 2013-04-03 16:11:29 +1100 | [diff] [blame] | 151 | const struct xfs_buf_ops xfs_dir3_free_buf_ops = { |
Eric Sandeen | 233135b | 2016-01-04 16:10:19 +1100 | [diff] [blame] | 152 | .name = "xfs_dir3_free", |
Brian Foster | 39708c2 | 2019-02-07 10:45:48 -0800 | [diff] [blame] | 153 | .magic = { cpu_to_be32(XFS_DIR2_FREE_MAGIC), |
| 154 | cpu_to_be32(XFS_DIR3_FREE_MAGIC) }, |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 155 | .verify_read = xfs_dir3_free_read_verify, |
| 156 | .verify_write = xfs_dir3_free_write_verify, |
Darrick J. Wong | b557259 | 2018-01-08 10:51:08 -0800 | [diff] [blame] | 157 | .verify_struct = xfs_dir3_free_verify, |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 158 | }; |
Dave Chinner | 2025207 | 2012-11-12 22:54:13 +1100 | [diff] [blame] | 159 | |
Darrick J. Wong | de14c5f | 2017-02-02 15:14:00 -0800 | [diff] [blame] | 160 | /* Everything ok in the free block header? */ |
Darrick J. Wong | bc1a09b | 2018-01-08 10:51:03 -0800 | [diff] [blame] | 161 | static xfs_failaddr_t |
Darrick J. Wong | de14c5f | 2017-02-02 15:14:00 -0800 | [diff] [blame] | 162 | xfs_dir3_free_header_check( |
| 163 | struct xfs_inode *dp, |
| 164 | xfs_dablk_t fbno, |
| 165 | struct xfs_buf *bp) |
| 166 | { |
| 167 | struct xfs_mount *mp = dp->i_mount; |
| 168 | unsigned int firstdb; |
| 169 | int maxbests; |
| 170 | |
| 171 | maxbests = dp->d_ops->free_max_bests(mp->m_dir_geo); |
| 172 | firstdb = (xfs_dir2_da_to_db(mp->m_dir_geo, fbno) - |
| 173 | xfs_dir2_byte_to_db(mp->m_dir_geo, XFS_DIR2_FREE_OFFSET)) * |
| 174 | maxbests; |
| 175 | if (xfs_sb_version_hascrc(&mp->m_sb)) { |
| 176 | struct xfs_dir3_free_hdr *hdr3 = bp->b_addr; |
| 177 | |
| 178 | if (be32_to_cpu(hdr3->firstdb) != firstdb) |
Darrick J. Wong | a6a781a | 2018-01-08 10:51:03 -0800 | [diff] [blame] | 179 | return __this_address; |
Darrick J. Wong | de14c5f | 2017-02-02 15:14:00 -0800 | [diff] [blame] | 180 | if (be32_to_cpu(hdr3->nvalid) > maxbests) |
Darrick J. Wong | a6a781a | 2018-01-08 10:51:03 -0800 | [diff] [blame] | 181 | return __this_address; |
Darrick J. Wong | de14c5f | 2017-02-02 15:14:00 -0800 | [diff] [blame] | 182 | if (be32_to_cpu(hdr3->nvalid) < be32_to_cpu(hdr3->nused)) |
Darrick J. Wong | a6a781a | 2018-01-08 10:51:03 -0800 | [diff] [blame] | 183 | return __this_address; |
Darrick J. Wong | de14c5f | 2017-02-02 15:14:00 -0800 | [diff] [blame] | 184 | } else { |
| 185 | struct xfs_dir2_free_hdr *hdr = bp->b_addr; |
| 186 | |
| 187 | if (be32_to_cpu(hdr->firstdb) != firstdb) |
Darrick J. Wong | a6a781a | 2018-01-08 10:51:03 -0800 | [diff] [blame] | 188 | return __this_address; |
Darrick J. Wong | de14c5f | 2017-02-02 15:14:00 -0800 | [diff] [blame] | 189 | if (be32_to_cpu(hdr->nvalid) > maxbests) |
Darrick J. Wong | a6a781a | 2018-01-08 10:51:03 -0800 | [diff] [blame] | 190 | return __this_address; |
Darrick J. Wong | de14c5f | 2017-02-02 15:14:00 -0800 | [diff] [blame] | 191 | if (be32_to_cpu(hdr->nvalid) < be32_to_cpu(hdr->nused)) |
Darrick J. Wong | a6a781a | 2018-01-08 10:51:03 -0800 | [diff] [blame] | 192 | return __this_address; |
Darrick J. Wong | de14c5f | 2017-02-02 15:14:00 -0800 | [diff] [blame] | 193 | } |
Darrick J. Wong | a6a781a | 2018-01-08 10:51:03 -0800 | [diff] [blame] | 194 | return NULL; |
Darrick J. Wong | de14c5f | 2017-02-02 15:14:00 -0800 | [diff] [blame] | 195 | } |
Dave Chinner | 612cfbf | 2012-11-14 17:52:32 +1100 | [diff] [blame] | 196 | |
Dave Chinner | 2025207 | 2012-11-12 22:54:13 +1100 | [diff] [blame] | 197 | static int |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 198 | __xfs_dir3_free_read( |
Dave Chinner | 2025207 | 2012-11-12 22:54:13 +1100 | [diff] [blame] | 199 | struct xfs_trans *tp, |
| 200 | struct xfs_inode *dp, |
| 201 | xfs_dablk_t fbno, |
| 202 | xfs_daddr_t mappedbno, |
| 203 | struct xfs_buf **bpp) |
| 204 | { |
Darrick J. Wong | bc1a09b | 2018-01-08 10:51:03 -0800 | [diff] [blame] | 205 | xfs_failaddr_t fa; |
Dave Chinner | d75afeb | 2013-04-03 16:11:29 +1100 | [diff] [blame] | 206 | int err; |
| 207 | |
| 208 | err = xfs_da_read_buf(tp, dp, fbno, mappedbno, bpp, |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 209 | XFS_DATA_FORK, &xfs_dir3_free_buf_ops); |
Darrick J. Wong | de14c5f | 2017-02-02 15:14:00 -0800 | [diff] [blame] | 210 | if (err || !*bpp) |
| 211 | return err; |
| 212 | |
| 213 | /* Check things that we can't do in the verifier. */ |
Darrick J. Wong | bc1a09b | 2018-01-08 10:51:03 -0800 | [diff] [blame] | 214 | fa = xfs_dir3_free_header_check(dp, fbno, *bpp); |
| 215 | if (fa) { |
| 216 | xfs_verifier_error(*bpp, -EFSCORRUPTED, fa); |
Darrick J. Wong | de14c5f | 2017-02-02 15:14:00 -0800 | [diff] [blame] | 217 | xfs_trans_brelse(tp, *bpp); |
| 218 | return -EFSCORRUPTED; |
| 219 | } |
Dave Chinner | d75afeb | 2013-04-03 16:11:29 +1100 | [diff] [blame] | 220 | |
| 221 | /* try read returns without an error or *bpp if it lands in a hole */ |
Darrick J. Wong | de14c5f | 2017-02-02 15:14:00 -0800 | [diff] [blame] | 222 | if (tp) |
Dave Chinner | 61fe135 | 2013-04-03 16:11:30 +1100 | [diff] [blame] | 223 | xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_FREE_BUF); |
Darrick J. Wong | de14c5f | 2017-02-02 15:14:00 -0800 | [diff] [blame] | 224 | |
| 225 | return 0; |
Dave Chinner | 2025207 | 2012-11-12 22:54:13 +1100 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | int |
| 229 | xfs_dir2_free_read( |
| 230 | struct xfs_trans *tp, |
| 231 | struct xfs_inode *dp, |
| 232 | xfs_dablk_t fbno, |
| 233 | struct xfs_buf **bpp) |
| 234 | { |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 235 | return __xfs_dir3_free_read(tp, dp, fbno, -1, bpp); |
Dave Chinner | 2025207 | 2012-11-12 22:54:13 +1100 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | static int |
| 239 | xfs_dir2_free_try_read( |
| 240 | struct xfs_trans *tp, |
| 241 | struct xfs_inode *dp, |
| 242 | xfs_dablk_t fbno, |
| 243 | struct xfs_buf **bpp) |
| 244 | { |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 245 | return __xfs_dir3_free_read(tp, dp, fbno, -2, bpp); |
| 246 | } |
| 247 | |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 248 | static int |
| 249 | xfs_dir3_free_get_buf( |
Dave Chinner | 2998ab1d | 2014-06-06 15:07:53 +1000 | [diff] [blame] | 250 | xfs_da_args_t *args, |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 251 | xfs_dir2_db_t fbno, |
| 252 | struct xfs_buf **bpp) |
| 253 | { |
Dave Chinner | 2998ab1d | 2014-06-06 15:07:53 +1000 | [diff] [blame] | 254 | struct xfs_trans *tp = args->trans; |
| 255 | struct xfs_inode *dp = args->dp; |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 256 | struct xfs_mount *mp = dp->i_mount; |
| 257 | struct xfs_buf *bp; |
| 258 | int error; |
| 259 | struct xfs_dir3_icfree_hdr hdr; |
| 260 | |
Dave Chinner | 2998ab1d | 2014-06-06 15:07:53 +1000 | [diff] [blame] | 261 | error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(args->geo, fbno), |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 262 | -1, &bp, XFS_DATA_FORK); |
| 263 | if (error) |
| 264 | return error; |
| 265 | |
Dave Chinner | 61fe135 | 2013-04-03 16:11:30 +1100 | [diff] [blame] | 266 | xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_FREE_BUF); |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 267 | bp->b_ops = &xfs_dir3_free_buf_ops; |
| 268 | |
| 269 | /* |
| 270 | * Initialize the new block to be empty, and remember |
| 271 | * its first slot as our empty slot. |
| 272 | */ |
Dave Chinner | e400d27 | 2013-05-28 18:37:17 +1000 | [diff] [blame] | 273 | memset(bp->b_addr, 0, sizeof(struct xfs_dir3_free_hdr)); |
| 274 | memset(&hdr, 0, sizeof(hdr)); |
| 275 | |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 276 | if (xfs_sb_version_hascrc(&mp->m_sb)) { |
| 277 | struct xfs_dir3_free_hdr *hdr3 = bp->b_addr; |
| 278 | |
| 279 | hdr.magic = XFS_DIR3_FREE_MAGIC; |
Dave Chinner | e400d27 | 2013-05-28 18:37:17 +1000 | [diff] [blame] | 280 | |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 281 | hdr3->hdr.blkno = cpu_to_be64(bp->b_bn); |
| 282 | hdr3->hdr.owner = cpu_to_be64(dp->i_ino); |
Eric Sandeen | ce748ea | 2015-07-29 11:53:31 +1000 | [diff] [blame] | 283 | uuid_copy(&hdr3->hdr.uuid, &mp->m_sb.sb_meta_uuid); |
Dave Chinner | e400d27 | 2013-05-28 18:37:17 +1000 | [diff] [blame] | 284 | } else |
| 285 | hdr.magic = XFS_DIR2_FREE_MAGIC; |
Dave Chinner | 01ba43b | 2013-10-29 22:11:52 +1100 | [diff] [blame] | 286 | dp->d_ops->free_hdr_to_disk(bp->b_addr, &hdr); |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 287 | *bpp = bp; |
| 288 | return 0; |
Dave Chinner | 2025207 | 2012-11-12 22:54:13 +1100 | [diff] [blame] | 289 | } |
| 290 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | /* |
| 292 | * Log entries from a freespace block. |
| 293 | */ |
Eric Sandeen | 5d77c0d | 2009-11-19 15:52:00 +0000 | [diff] [blame] | 294 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | xfs_dir2_free_log_bests( |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame] | 296 | struct xfs_da_args *args, |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 297 | struct xfs_buf *bp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | int first, /* first entry to log */ |
| 299 | int last) /* last entry to log */ |
| 300 | { |
| 301 | xfs_dir2_free_t *free; /* freespace structure */ |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 302 | __be16 *bests; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 304 | free = bp->b_addr; |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame] | 305 | bests = args->dp->d_ops->free_bests_p(free); |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 306 | ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) || |
| 307 | free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC)); |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame] | 308 | xfs_trans_log_buf(args->trans, bp, |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 309 | (uint)((char *)&bests[first] - (char *)free), |
| 310 | (uint)((char *)&bests[last] - (char *)free + |
| 311 | sizeof(bests[0]) - 1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | /* |
| 315 | * Log header from a freespace block. |
| 316 | */ |
| 317 | static void |
| 318 | xfs_dir2_free_log_header( |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame] | 319 | struct xfs_da_args *args, |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 320 | struct xfs_buf *bp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | { |
Dave Chinner | 836a94a | 2013-08-12 20:49:44 +1000 | [diff] [blame] | 322 | #ifdef DEBUG |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | xfs_dir2_free_t *free; /* freespace structure */ |
| 324 | |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 325 | free = bp->b_addr; |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 326 | ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) || |
| 327 | free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC)); |
Dave Chinner | 836a94a | 2013-08-12 20:49:44 +1000 | [diff] [blame] | 328 | #endif |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame] | 329 | xfs_trans_log_buf(args->trans, bp, 0, |
| 330 | args->dp->d_ops->free_hdr_size - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | /* |
| 334 | * Convert a leaf-format directory to a node-format directory. |
| 335 | * We need to change the magic number of the leaf block, and copy |
| 336 | * the freespace table out of the leaf block into its own block. |
| 337 | */ |
| 338 | int /* error */ |
| 339 | xfs_dir2_leaf_to_node( |
| 340 | xfs_da_args_t *args, /* operation arguments */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 341 | struct xfs_buf *lbp) /* leaf buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | { |
| 343 | xfs_inode_t *dp; /* incore directory inode */ |
| 344 | int error; /* error return value */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 345 | struct xfs_buf *fbp; /* freespace buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | xfs_dir2_db_t fdb; /* freespace block number */ |
| 347 | xfs_dir2_free_t *free; /* freespace structure */ |
Nathan Scott | 68b3a10 | 2006-03-17 17:27:19 +1100 | [diff] [blame] | 348 | __be16 *from; /* pointer to freespace entry */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | int i; /* leaf freespace index */ |
| 350 | xfs_dir2_leaf_t *leaf; /* leaf structure */ |
| 351 | xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | int n; /* count of live freespc ents */ |
| 353 | xfs_dir2_data_off_t off; /* freespace entry value */ |
Nathan Scott | 0ba962e | 2006-03-17 17:27:07 +1100 | [diff] [blame] | 354 | __be16 *to; /* pointer to freespace entry */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | xfs_trans_t *tp; /* transaction pointer */ |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 356 | struct xfs_dir3_icfree_hdr freehdr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 358 | trace_xfs_dir2_leaf_to_node(args); |
| 359 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | dp = args->dp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | tp = args->trans; |
| 362 | /* |
| 363 | * Add a freespace block to the directory. |
| 364 | */ |
| 365 | if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fdb))) { |
| 366 | return error; |
| 367 | } |
Dave Chinner | 3002803 | 2014-06-06 15:08:18 +1000 | [diff] [blame] | 368 | ASSERT(fdb == xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | /* |
| 370 | * Get the buffer for the new freespace block. |
| 371 | */ |
Dave Chinner | 2998ab1d | 2014-06-06 15:07:53 +1000 | [diff] [blame] | 372 | error = xfs_dir3_free_get_buf(args, fdb, &fbp); |
Dave Chinner | b0f539d | 2012-11-14 17:53:49 +1100 | [diff] [blame] | 373 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | return error; |
Dave Chinner | b0f539d | 2012-11-14 17:53:49 +1100 | [diff] [blame] | 375 | |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 376 | free = fbp->b_addr; |
Dave Chinner | 01ba43b | 2013-10-29 22:11:52 +1100 | [diff] [blame] | 377 | dp->d_ops->free_hdr_from_disk(&freehdr, free); |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 378 | leaf = lbp->b_addr; |
Dave Chinner | 8f66193 | 2014-06-06 15:15:59 +1000 | [diff] [blame] | 379 | ltp = xfs_dir2_leaf_tail_p(args->geo, leaf); |
Darrick J. Wong | 3f883f5 | 2018-03-06 17:08:31 -0800 | [diff] [blame] | 380 | if (be32_to_cpu(ltp->bestcount) > |
| 381 | (uint)dp->i_d.di_size / args->geo->blksize) |
| 382 | return -EFSCORRUPTED; |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 383 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | /* |
| 385 | * Copy freespace entries from the leaf block to the new block. |
| 386 | * Count active entries. |
| 387 | */ |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 388 | from = xfs_dir2_leaf_bests_p(ltp); |
Dave Chinner | 24dd0f5 | 2013-10-30 13:48:41 -0500 | [diff] [blame] | 389 | to = dp->d_ops->free_bests_p(free); |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 390 | for (i = n = 0; i < be32_to_cpu(ltp->bestcount); i++, from++, to++) { |
Nathan Scott | 68b3a10 | 2006-03-17 17:27:19 +1100 | [diff] [blame] | 391 | if ((off = be16_to_cpu(*from)) != NULLDATAOFF) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | n++; |
Nathan Scott | 0ba962e | 2006-03-17 17:27:07 +1100 | [diff] [blame] | 393 | *to = cpu_to_be16(off); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | } |
Dave Chinner | b0f539d | 2012-11-14 17:53:49 +1100 | [diff] [blame] | 395 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | /* |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 397 | * Now initialize the freespace block header. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | */ |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 399 | freehdr.nused = n; |
| 400 | freehdr.nvalid = be32_to_cpu(ltp->bestcount); |
| 401 | |
Dave Chinner | 01ba43b | 2013-10-29 22:11:52 +1100 | [diff] [blame] | 402 | dp->d_ops->free_hdr_to_disk(fbp->b_addr, &freehdr); |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame] | 403 | xfs_dir2_free_log_bests(args, fbp, 0, freehdr.nvalid - 1); |
| 404 | xfs_dir2_free_log_header(args, fbp); |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 405 | |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 406 | /* |
| 407 | * Converting the leaf to a leafnode is just a matter of changing the |
| 408 | * magic number and the ops. Do the change directly to the buffer as |
| 409 | * it's less work (and less code) than decoding the header to host |
| 410 | * format and back again. |
| 411 | */ |
| 412 | if (leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC)) |
| 413 | leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAFN_MAGIC); |
| 414 | else |
| 415 | leaf->hdr.info.magic = cpu_to_be16(XFS_DIR3_LEAFN_MAGIC); |
| 416 | lbp->b_ops = &xfs_dir3_leafn_buf_ops; |
Dave Chinner | 61fe135 | 2013-04-03 16:11:30 +1100 | [diff] [blame] | 417 | xfs_trans_buf_set_type(tp, lbp, XFS_BLFT_DIR_LEAFN_BUF); |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame] | 418 | xfs_dir3_leaf_log_header(args, lbp); |
Dave Chinner | 4141956 | 2013-10-29 22:11:50 +1100 | [diff] [blame] | 419 | xfs_dir3_leaf_check(dp, lbp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | return 0; |
| 421 | } |
| 422 | |
| 423 | /* |
| 424 | * Add a leaf entry to a leaf block in a node-form directory. |
| 425 | * The other work necessary is done from the caller. |
| 426 | */ |
| 427 | static int /* error */ |
| 428 | xfs_dir2_leafn_add( |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 429 | struct xfs_buf *bp, /* leaf buffer */ |
Darrick J. Wong | 79622c7ce | 2019-03-07 16:50:11 -0800 | [diff] [blame] | 430 | struct xfs_da_args *args, /* operation arguments */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | int index) /* insertion pt for new entry */ |
| 432 | { |
Darrick J. Wong | 79622c7ce | 2019-03-07 16:50:11 -0800 | [diff] [blame] | 433 | struct xfs_dir3_icleaf_hdr leafhdr; |
| 434 | struct xfs_inode *dp = args->dp; |
| 435 | struct xfs_dir2_leaf *leaf = bp->b_addr; |
| 436 | struct xfs_dir2_leaf_entry *lep; |
| 437 | struct xfs_dir2_leaf_entry *ents; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | int compact; /* compacting stale leaves */ |
Darrick J. Wong | 79622c7ce | 2019-03-07 16:50:11 -0800 | [diff] [blame] | 439 | int highstale = 0; /* next stale entry */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | int lfloghigh; /* high leaf entry logging */ |
| 441 | int lfloglow; /* low leaf entry logging */ |
Darrick J. Wong | 79622c7ce | 2019-03-07 16:50:11 -0800 | [diff] [blame] | 442 | int lowstale = 0; /* previous stale entry */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 444 | trace_xfs_dir2_leafn_add(args, index); |
| 445 | |
Dave Chinner | 01ba43b | 2013-10-29 22:11:52 +1100 | [diff] [blame] | 446 | dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf); |
Dave Chinner | 4141956 | 2013-10-29 22:11:50 +1100 | [diff] [blame] | 447 | ents = dp->d_ops->leaf_ents_p(leaf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | |
| 449 | /* |
| 450 | * Quick check just to make sure we are not going to index |
| 451 | * into other peoples memory |
| 452 | */ |
| 453 | if (index < 0) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 454 | return -EFSCORRUPTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | |
| 456 | /* |
| 457 | * If there are already the maximum number of leaf entries in |
| 458 | * the block, if there are no stale entries it won't fit. |
| 459 | * Caller will do a split. If there are stale entries we'll do |
| 460 | * a compact. |
| 461 | */ |
| 462 | |
Dave Chinner | 8f66193 | 2014-06-06 15:15:59 +1000 | [diff] [blame] | 463 | if (leafhdr.count == dp->d_ops->leaf_max_ents(args->geo)) { |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 464 | if (!leafhdr.stale) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 465 | return -ENOSPC; |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 466 | compact = leafhdr.stale > 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | } else |
| 468 | compact = 0; |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 469 | ASSERT(index == 0 || be32_to_cpu(ents[index - 1].hashval) <= args->hashval); |
| 470 | ASSERT(index == leafhdr.count || |
| 471 | be32_to_cpu(ents[index].hashval) >= args->hashval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | |
Barry Naujok | 6a17810 | 2008-05-21 16:42:05 +1000 | [diff] [blame] | 473 | if (args->op_flags & XFS_DA_OP_JUSTCHECK) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | return 0; |
| 475 | |
| 476 | /* |
| 477 | * Compact out all but one stale leaf entry. Leaves behind |
| 478 | * the entry closest to index. |
| 479 | */ |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 480 | if (compact) |
| 481 | xfs_dir3_leaf_compact_x1(&leafhdr, ents, &index, &lowstale, |
| 482 | &highstale, &lfloglow, &lfloghigh); |
| 483 | else if (leafhdr.stale) { |
| 484 | /* |
| 485 | * Set impossible logging indices for this case. |
| 486 | */ |
| 487 | lfloglow = leafhdr.count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | lfloghigh = -1; |
| 489 | } |
Christoph Hellwig | 4fb44c8 | 2011-07-08 14:34:59 +0200 | [diff] [blame] | 490 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | /* |
| 492 | * Insert the new entry, log everything. |
| 493 | */ |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 494 | lep = xfs_dir3_leaf_find_entry(&leafhdr, ents, index, compact, lowstale, |
Christoph Hellwig | 4fb44c8 | 2011-07-08 14:34:59 +0200 | [diff] [blame] | 495 | highstale, &lfloglow, &lfloghigh); |
| 496 | |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 497 | lep->hashval = cpu_to_be32(args->hashval); |
Dave Chinner | 3002803 | 2014-06-06 15:08:18 +1000 | [diff] [blame] | 498 | lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(args->geo, |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 499 | args->blkno, args->index)); |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 500 | |
Dave Chinner | 01ba43b | 2013-10-29 22:11:52 +1100 | [diff] [blame] | 501 | dp->d_ops->leaf_hdr_to_disk(leaf, &leafhdr); |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame] | 502 | xfs_dir3_leaf_log_header(args, bp); |
| 503 | xfs_dir3_leaf_log_ents(args, bp, lfloglow, lfloghigh); |
Dave Chinner | 4141956 | 2013-10-29 22:11:50 +1100 | [diff] [blame] | 504 | xfs_dir3_leaf_check(dp, bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | return 0; |
| 506 | } |
| 507 | |
| 508 | #ifdef DEBUG |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 509 | static void |
| 510 | xfs_dir2_free_hdr_check( |
Dave Chinner | 01ba43b | 2013-10-29 22:11:52 +1100 | [diff] [blame] | 511 | struct xfs_inode *dp, |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 512 | struct xfs_buf *bp, |
| 513 | xfs_dir2_db_t db) |
| 514 | { |
| 515 | struct xfs_dir3_icfree_hdr hdr; |
| 516 | |
Dave Chinner | 01ba43b | 2013-10-29 22:11:52 +1100 | [diff] [blame] | 517 | dp->d_ops->free_hdr_from_disk(&hdr, bp->b_addr); |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 518 | |
Dave Chinner | 8f66193 | 2014-06-06 15:15:59 +1000 | [diff] [blame] | 519 | ASSERT((hdr.firstdb % |
| 520 | dp->d_ops->free_max_bests(dp->i_mount->m_dir_geo)) == 0); |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 521 | ASSERT(hdr.firstdb <= db); |
| 522 | ASSERT(db < hdr.firstdb + hdr.nvalid); |
| 523 | } |
| 524 | #else |
Dave Chinner | 01ba43b | 2013-10-29 22:11:52 +1100 | [diff] [blame] | 525 | #define xfs_dir2_free_hdr_check(dp, bp, db) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | #endif /* DEBUG */ |
| 527 | |
| 528 | /* |
| 529 | * Return the last hash value in the leaf. |
| 530 | * Stale entries are ok. |
| 531 | */ |
| 532 | xfs_dahash_t /* hash value */ |
Darrick J. Wong | 8e8877e | 2017-06-16 11:00:13 -0700 | [diff] [blame] | 533 | xfs_dir2_leaf_lasthash( |
Dave Chinner | 4141956 | 2013-10-29 22:11:50 +1100 | [diff] [blame] | 534 | struct xfs_inode *dp, |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 535 | struct xfs_buf *bp, /* leaf buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | int *count) /* count of entries in leaf */ |
| 537 | { |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 538 | struct xfs_dir2_leaf *leaf = bp->b_addr; |
| 539 | struct xfs_dir2_leaf_entry *ents; |
| 540 | struct xfs_dir3_icleaf_hdr leafhdr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | |
Dave Chinner | 01ba43b | 2013-10-29 22:11:52 +1100 | [diff] [blame] | 542 | dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf); |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 543 | |
| 544 | ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC || |
Darrick J. Wong | 8e8877e | 2017-06-16 11:00:13 -0700 | [diff] [blame] | 545 | leafhdr.magic == XFS_DIR3_LEAFN_MAGIC || |
| 546 | leafhdr.magic == XFS_DIR2_LEAF1_MAGIC || |
| 547 | leafhdr.magic == XFS_DIR3_LEAF1_MAGIC); |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 548 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | if (count) |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 550 | *count = leafhdr.count; |
| 551 | if (!leafhdr.count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | return 0; |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 553 | |
Dave Chinner | 4141956 | 2013-10-29 22:11:50 +1100 | [diff] [blame] | 554 | ents = dp->d_ops->leaf_ents_p(leaf); |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 555 | return be32_to_cpu(ents[leafhdr.count - 1].hashval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | /* |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 559 | * Look up a leaf entry for space to add a name in a node-format leaf block. |
| 560 | * The extrablk in state is a freespace block. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | */ |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 562 | STATIC int |
| 563 | xfs_dir2_leafn_lookup_for_addname( |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 564 | struct xfs_buf *bp, /* leaf buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | xfs_da_args_t *args, /* operation arguments */ |
| 566 | int *indexp, /* out: leaf entry index */ |
| 567 | xfs_da_state_t *state) /* state to fill in */ |
| 568 | { |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 569 | struct xfs_buf *curbp = NULL; /* current data/free buffer */ |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 570 | xfs_dir2_db_t curdb = -1; /* current data block number */ |
| 571 | xfs_dir2_db_t curfdb = -1; /* current free block number */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | xfs_inode_t *dp; /* incore directory inode */ |
| 573 | int error; /* error return value */ |
| 574 | int fi; /* free entry index */ |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 575 | xfs_dir2_free_t *free = NULL; /* free block structure */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | int index; /* leaf entry index */ |
| 577 | xfs_dir2_leaf_t *leaf; /* leaf structure */ |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 578 | int length; /* length of new data entry */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | xfs_dir2_leaf_entry_t *lep; /* leaf entry */ |
| 580 | xfs_mount_t *mp; /* filesystem mount point */ |
| 581 | xfs_dir2_db_t newdb; /* new data block number */ |
| 582 | xfs_dir2_db_t newfdb; /* new free block number */ |
| 583 | xfs_trans_t *tp; /* transaction pointer */ |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 584 | struct xfs_dir2_leaf_entry *ents; |
| 585 | struct xfs_dir3_icleaf_hdr leafhdr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | |
| 587 | dp = args->dp; |
| 588 | tp = args->trans; |
| 589 | mp = dp->i_mount; |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 590 | leaf = bp->b_addr; |
Dave Chinner | 01ba43b | 2013-10-29 22:11:52 +1100 | [diff] [blame] | 591 | dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf); |
Dave Chinner | 4141956 | 2013-10-29 22:11:50 +1100 | [diff] [blame] | 592 | ents = dp->d_ops->leaf_ents_p(leaf); |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 593 | |
Dave Chinner | 4141956 | 2013-10-29 22:11:50 +1100 | [diff] [blame] | 594 | xfs_dir3_leaf_check(dp, bp); |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 595 | ASSERT(leafhdr.count > 0); |
| 596 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | /* |
| 598 | * Look up the hash value in the leaf entries. |
| 599 | */ |
| 600 | index = xfs_dir2_leaf_search_hash(args, bp); |
| 601 | /* |
| 602 | * Do we have a buffer coming in? |
| 603 | */ |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 604 | if (state->extravalid) { |
| 605 | /* If so, it's a free block buffer, get the block number. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | curbp = state->extrablk.bp; |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 607 | curfdb = state->extrablk.blkno; |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 608 | free = curbp->b_addr; |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 609 | ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) || |
| 610 | free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | } |
Dave Chinner | 9d23fc8 | 2013-10-29 22:11:48 +1100 | [diff] [blame] | 612 | length = dp->d_ops->data_entsize(args->namelen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | /* |
| 614 | * Loop over leaf entries with the right hash value. |
| 615 | */ |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 616 | for (lep = &ents[index]; |
| 617 | index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval; |
| 618 | lep++, index++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | /* |
| 620 | * Skip stale leaf entries. |
| 621 | */ |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 622 | if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | continue; |
| 624 | /* |
| 625 | * Pull the data block number from the entry. |
| 626 | */ |
Dave Chinner | 3002803 | 2014-06-06 15:08:18 +1000 | [diff] [blame] | 627 | newdb = xfs_dir2_dataptr_to_db(args->geo, |
| 628 | be32_to_cpu(lep->address)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | /* |
| 630 | * For addname, we're looking for a place to put the new entry. |
| 631 | * We want to use a data block with an entry of equal |
| 632 | * hash value to ours if there is one with room. |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 633 | * |
| 634 | * If this block isn't the data block we already have |
| 635 | * in hand, take a look at it. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | */ |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 637 | if (newdb != curdb) { |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 638 | __be16 *bests; |
| 639 | |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 640 | curdb = newdb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | /* |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 642 | * Convert the data block to the free block |
| 643 | * holding its freespace information. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | */ |
Dave Chinner | 8f66193 | 2014-06-06 15:15:59 +1000 | [diff] [blame] | 645 | newfdb = dp->d_ops->db_to_fdb(args->geo, newdb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | /* |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 647 | * If it's not the one we have in hand, read it in. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | */ |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 649 | if (newfdb != curfdb) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | /* |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 651 | * If we had one before, drop it. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | */ |
| 653 | if (curbp) |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 654 | xfs_trans_brelse(tp, curbp); |
Dave Chinner | 2025207 | 2012-11-12 22:54:13 +1100 | [diff] [blame] | 655 | |
| 656 | error = xfs_dir2_free_read(tp, dp, |
Dave Chinner | 2998ab1d | 2014-06-06 15:07:53 +1000 | [diff] [blame] | 657 | xfs_dir2_db_to_da(args->geo, |
| 658 | newfdb), |
Dave Chinner | 2025207 | 2012-11-12 22:54:13 +1100 | [diff] [blame] | 659 | &curbp); |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 660 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | return error; |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 662 | free = curbp->b_addr; |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 663 | |
Dave Chinner | 01ba43b | 2013-10-29 22:11:52 +1100 | [diff] [blame] | 664 | xfs_dir2_free_hdr_check(dp, curbp, curdb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | } |
| 666 | /* |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 667 | * Get the index for our entry. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | */ |
Dave Chinner | 8f66193 | 2014-06-06 15:15:59 +1000 | [diff] [blame] | 669 | fi = dp->d_ops->db_to_fdindex(args->geo, curdb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | /* |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 671 | * If it has room, return it. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | */ |
Dave Chinner | 24dd0f5 | 2013-10-30 13:48:41 -0500 | [diff] [blame] | 673 | bests = dp->d_ops->free_bests_p(free); |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 674 | if (unlikely(bests[fi] == cpu_to_be16(NULLDATAOFF))) { |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 675 | XFS_ERROR_REPORT("xfs_dir2_leafn_lookup_int", |
| 676 | XFS_ERRLEVEL_LOW, mp); |
| 677 | if (curfdb != newfdb) |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 678 | xfs_trans_brelse(tp, curbp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 679 | return -EFSCORRUPTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | } |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 681 | curfdb = newfdb; |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 682 | if (be16_to_cpu(bests[fi]) >= length) |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 683 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | } |
| 685 | } |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 686 | /* Didn't find any space */ |
| 687 | fi = -1; |
| 688 | out: |
Barry Naujok | 6a17810 | 2008-05-21 16:42:05 +1000 | [diff] [blame] | 689 | ASSERT(args->op_flags & XFS_DA_OP_OKNOENT); |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 690 | if (curbp) { |
| 691 | /* Giving back a free block. */ |
| 692 | state->extravalid = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | state->extrablk.bp = curbp; |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 694 | state->extrablk.index = fi; |
| 695 | state->extrablk.blkno = curfdb; |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 696 | |
| 697 | /* |
| 698 | * Important: this magic number is not in the buffer - it's for |
| 699 | * buffer type information and therefore only the free/data type |
| 700 | * matters here, not whether CRCs are enabled or not. |
| 701 | */ |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 702 | state->extrablk.magic = XFS_DIR2_FREE_MAGIC; |
| 703 | } else { |
| 704 | state->extravalid = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | } |
| 706 | /* |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 707 | * Return the index, that will be the insertion point. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | */ |
| 709 | *indexp = index; |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 710 | return -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | /* |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 714 | * Look up a leaf entry in a node-format leaf block. |
| 715 | * The extrablk in state a data block. |
| 716 | */ |
| 717 | STATIC int |
| 718 | xfs_dir2_leafn_lookup_for_entry( |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 719 | struct xfs_buf *bp, /* leaf buffer */ |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 720 | xfs_da_args_t *args, /* operation arguments */ |
| 721 | int *indexp, /* out: leaf entry index */ |
| 722 | xfs_da_state_t *state) /* state to fill in */ |
| 723 | { |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 724 | struct xfs_buf *curbp = NULL; /* current data/free buffer */ |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 725 | xfs_dir2_db_t curdb = -1; /* current data block number */ |
| 726 | xfs_dir2_data_entry_t *dep; /* data block entry */ |
| 727 | xfs_inode_t *dp; /* incore directory inode */ |
| 728 | int error; /* error return value */ |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 729 | int index; /* leaf entry index */ |
| 730 | xfs_dir2_leaf_t *leaf; /* leaf structure */ |
| 731 | xfs_dir2_leaf_entry_t *lep; /* leaf entry */ |
| 732 | xfs_mount_t *mp; /* filesystem mount point */ |
| 733 | xfs_dir2_db_t newdb; /* new data block number */ |
| 734 | xfs_trans_t *tp; /* transaction pointer */ |
Barry Naujok | 5163f95 | 2008-05-21 16:41:01 +1000 | [diff] [blame] | 735 | enum xfs_dacmp cmp; /* comparison result */ |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 736 | struct xfs_dir2_leaf_entry *ents; |
| 737 | struct xfs_dir3_icleaf_hdr leafhdr; |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 738 | |
| 739 | dp = args->dp; |
| 740 | tp = args->trans; |
| 741 | mp = dp->i_mount; |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 742 | leaf = bp->b_addr; |
Dave Chinner | 01ba43b | 2013-10-29 22:11:52 +1100 | [diff] [blame] | 743 | dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf); |
Dave Chinner | 4141956 | 2013-10-29 22:11:50 +1100 | [diff] [blame] | 744 | ents = dp->d_ops->leaf_ents_p(leaf); |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 745 | |
Dave Chinner | 4141956 | 2013-10-29 22:11:50 +1100 | [diff] [blame] | 746 | xfs_dir3_leaf_check(dp, bp); |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 747 | ASSERT(leafhdr.count > 0); |
| 748 | |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 749 | /* |
| 750 | * Look up the hash value in the leaf entries. |
| 751 | */ |
| 752 | index = xfs_dir2_leaf_search_hash(args, bp); |
| 753 | /* |
| 754 | * Do we have a buffer coming in? |
| 755 | */ |
| 756 | if (state->extravalid) { |
| 757 | curbp = state->extrablk.bp; |
| 758 | curdb = state->extrablk.blkno; |
| 759 | } |
| 760 | /* |
| 761 | * Loop over leaf entries with the right hash value. |
| 762 | */ |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 763 | for (lep = &ents[index]; |
| 764 | index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval; |
| 765 | lep++, index++) { |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 766 | /* |
| 767 | * Skip stale leaf entries. |
| 768 | */ |
| 769 | if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR) |
| 770 | continue; |
| 771 | /* |
| 772 | * Pull the data block number from the entry. |
| 773 | */ |
Dave Chinner | 3002803 | 2014-06-06 15:08:18 +1000 | [diff] [blame] | 774 | newdb = xfs_dir2_dataptr_to_db(args->geo, |
| 775 | be32_to_cpu(lep->address)); |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 776 | /* |
| 777 | * Not adding a new entry, so we really want to find |
| 778 | * the name given to us. |
| 779 | * |
| 780 | * If it's a different data block, go get it. |
| 781 | */ |
| 782 | if (newdb != curdb) { |
| 783 | /* |
Barry Naujok | 90bb7ab | 2008-06-23 13:25:38 +1000 | [diff] [blame] | 784 | * If we had a block before that we aren't saving |
| 785 | * for a CI name, drop it |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 786 | */ |
Barry Naujok | 90bb7ab | 2008-06-23 13:25:38 +1000 | [diff] [blame] | 787 | if (curbp && (args->cmpresult == XFS_CMP_DIFFERENT || |
| 788 | curdb != state->extrablk.blkno)) |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 789 | xfs_trans_brelse(tp, curbp); |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 790 | /* |
Barry Naujok | 90bb7ab | 2008-06-23 13:25:38 +1000 | [diff] [blame] | 791 | * If needing the block that is saved with a CI match, |
| 792 | * use it otherwise read in the new data block. |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 793 | */ |
Barry Naujok | 90bb7ab | 2008-06-23 13:25:38 +1000 | [diff] [blame] | 794 | if (args->cmpresult != XFS_CMP_DIFFERENT && |
| 795 | newdb == state->extrablk.blkno) { |
| 796 | ASSERT(state->extravalid); |
| 797 | curbp = state->extrablk.bp; |
| 798 | } else { |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame] | 799 | error = xfs_dir3_data_read(tp, dp, |
Dave Chinner | 2998ab1d | 2014-06-06 15:07:53 +1000 | [diff] [blame] | 800 | xfs_dir2_db_to_da(args->geo, |
| 801 | newdb), |
Dave Chinner | e481357 | 2012-11-12 22:54:14 +1100 | [diff] [blame] | 802 | -1, &curbp); |
Barry Naujok | 90bb7ab | 2008-06-23 13:25:38 +1000 | [diff] [blame] | 803 | if (error) |
| 804 | return error; |
| 805 | } |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame] | 806 | xfs_dir3_data_check(dp, curbp); |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 807 | curdb = newdb; |
| 808 | } |
| 809 | /* |
| 810 | * Point to the data entry. |
| 811 | */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 812 | dep = (xfs_dir2_data_entry_t *)((char *)curbp->b_addr + |
Dave Chinner | 3002803 | 2014-06-06 15:08:18 +1000 | [diff] [blame] | 813 | xfs_dir2_dataptr_to_off(args->geo, |
| 814 | be32_to_cpu(lep->address))); |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 815 | /* |
Barry Naujok | 5163f95 | 2008-05-21 16:41:01 +1000 | [diff] [blame] | 816 | * Compare the entry and if it's an exact match, return |
| 817 | * EEXIST immediately. If it's the first case-insensitive |
Barry Naujok | 90bb7ab | 2008-06-23 13:25:38 +1000 | [diff] [blame] | 818 | * match, store the block & inode number and continue looking. |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 819 | */ |
Barry Naujok | 5163f95 | 2008-05-21 16:41:01 +1000 | [diff] [blame] | 820 | cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen); |
| 821 | if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) { |
Barry Naujok | 90bb7ab | 2008-06-23 13:25:38 +1000 | [diff] [blame] | 822 | /* If there is a CI match block, drop it */ |
| 823 | if (args->cmpresult != XFS_CMP_DIFFERENT && |
| 824 | curdb != state->extrablk.blkno) |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 825 | xfs_trans_brelse(tp, state->extrablk.bp); |
Barry Naujok | 5163f95 | 2008-05-21 16:41:01 +1000 | [diff] [blame] | 826 | args->cmpresult = cmp; |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 827 | args->inumber = be64_to_cpu(dep->inumber); |
Dave Chinner | 9d23fc8 | 2013-10-29 22:11:48 +1100 | [diff] [blame] | 828 | args->filetype = dp->d_ops->data_get_ftype(dep); |
Barry Naujok | 90bb7ab | 2008-06-23 13:25:38 +1000 | [diff] [blame] | 829 | *indexp = index; |
| 830 | state->extravalid = 1; |
| 831 | state->extrablk.bp = curbp; |
| 832 | state->extrablk.blkno = curdb; |
| 833 | state->extrablk.index = (int)((char *)dep - |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 834 | (char *)curbp->b_addr); |
Barry Naujok | 90bb7ab | 2008-06-23 13:25:38 +1000 | [diff] [blame] | 835 | state->extrablk.magic = XFS_DIR2_DATA_MAGIC; |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame] | 836 | curbp->b_ops = &xfs_dir3_data_buf_ops; |
Dave Chinner | 61fe135 | 2013-04-03 16:11:30 +1100 | [diff] [blame] | 837 | xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF); |
Barry Naujok | 5163f95 | 2008-05-21 16:41:01 +1000 | [diff] [blame] | 838 | if (cmp == XFS_CMP_EXACT) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 839 | return -EEXIST; |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 840 | } |
| 841 | } |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 842 | ASSERT(index == leafhdr.count || (args->op_flags & XFS_DA_OP_OKNOENT)); |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 843 | if (curbp) { |
Barry Naujok | 90bb7ab | 2008-06-23 13:25:38 +1000 | [diff] [blame] | 844 | if (args->cmpresult == XFS_CMP_DIFFERENT) { |
| 845 | /* Giving back last used data block. */ |
| 846 | state->extravalid = 1; |
| 847 | state->extrablk.bp = curbp; |
| 848 | state->extrablk.index = -1; |
| 849 | state->extrablk.blkno = curdb; |
| 850 | state->extrablk.magic = XFS_DIR2_DATA_MAGIC; |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame] | 851 | curbp->b_ops = &xfs_dir3_data_buf_ops; |
Dave Chinner | 61fe135 | 2013-04-03 16:11:30 +1100 | [diff] [blame] | 852 | xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF); |
Barry Naujok | 90bb7ab | 2008-06-23 13:25:38 +1000 | [diff] [blame] | 853 | } else { |
| 854 | /* If the curbp is not the CI match block, drop it */ |
| 855 | if (state->extrablk.bp != curbp) |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 856 | xfs_trans_brelse(tp, curbp); |
Barry Naujok | 90bb7ab | 2008-06-23 13:25:38 +1000 | [diff] [blame] | 857 | } |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 858 | } else { |
| 859 | state->extravalid = 0; |
| 860 | } |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 861 | *indexp = index; |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 862 | return -ENOENT; |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 863 | } |
| 864 | |
| 865 | /* |
| 866 | * Look up a leaf entry in a node-format leaf block. |
| 867 | * If this is an addname then the extrablk in state is a freespace block, |
| 868 | * otherwise it's a data block. |
| 869 | */ |
| 870 | int |
| 871 | xfs_dir2_leafn_lookup_int( |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 872 | struct xfs_buf *bp, /* leaf buffer */ |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 873 | xfs_da_args_t *args, /* operation arguments */ |
| 874 | int *indexp, /* out: leaf entry index */ |
| 875 | xfs_da_state_t *state) /* state to fill in */ |
| 876 | { |
Barry Naujok | 6a17810 | 2008-05-21 16:42:05 +1000 | [diff] [blame] | 877 | if (args->op_flags & XFS_DA_OP_ADDNAME) |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 878 | return xfs_dir2_leafn_lookup_for_addname(bp, args, indexp, |
| 879 | state); |
| 880 | return xfs_dir2_leafn_lookup_for_entry(bp, args, indexp, state); |
| 881 | } |
| 882 | |
| 883 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | * Move count leaf entries from source to destination leaf. |
| 885 | * Log entries and headers. Stale entries are preserved. |
| 886 | */ |
| 887 | static void |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 888 | xfs_dir3_leafn_moveents( |
| 889 | xfs_da_args_t *args, /* operation arguments */ |
| 890 | struct xfs_buf *bp_s, /* source */ |
| 891 | struct xfs_dir3_icleaf_hdr *shdr, |
| 892 | struct xfs_dir2_leaf_entry *sents, |
| 893 | int start_s,/* source leaf index */ |
| 894 | struct xfs_buf *bp_d, /* destination */ |
| 895 | struct xfs_dir3_icleaf_hdr *dhdr, |
| 896 | struct xfs_dir2_leaf_entry *dents, |
| 897 | int start_d,/* destination leaf index */ |
| 898 | int count) /* count of leaves to copy */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | { |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 900 | int stale; /* count stale leaves copied */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 902 | trace_xfs_dir2_leafn_moveents(args, start_s, start_d, count); |
| 903 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 904 | /* |
| 905 | * Silently return if nothing to do. |
| 906 | */ |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 907 | if (count == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 908 | return; |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 909 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | /* |
| 911 | * If the destination index is not the end of the current |
| 912 | * destination leaf entries, open up a hole in the destination |
| 913 | * to hold the new entries. |
| 914 | */ |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 915 | if (start_d < dhdr->count) { |
| 916 | memmove(&dents[start_d + count], &dents[start_d], |
| 917 | (dhdr->count - start_d) * sizeof(xfs_dir2_leaf_entry_t)); |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame] | 918 | xfs_dir3_leaf_log_ents(args, bp_d, start_d + count, |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 919 | count + dhdr->count - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 920 | } |
| 921 | /* |
| 922 | * If the source has stale leaves, count the ones in the copy range |
| 923 | * so we can update the header correctly. |
| 924 | */ |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 925 | if (shdr->stale) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | int i; /* temp leaf index */ |
| 927 | |
| 928 | for (i = start_s, stale = 0; i < start_s + count; i++) { |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 929 | if (sents[i].address == |
| 930 | cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | stale++; |
| 932 | } |
| 933 | } else |
| 934 | stale = 0; |
| 935 | /* |
| 936 | * Copy the leaf entries from source to destination. |
| 937 | */ |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 938 | memcpy(&dents[start_d], &sents[start_s], |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | count * sizeof(xfs_dir2_leaf_entry_t)); |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame] | 940 | xfs_dir3_leaf_log_ents(args, bp_d, start_d, start_d + count - 1); |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 941 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | /* |
| 943 | * If there are source entries after the ones we copied, |
| 944 | * delete the ones we copied by sliding the next ones down. |
| 945 | */ |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 946 | if (start_s + count < shdr->count) { |
| 947 | memmove(&sents[start_s], &sents[start_s + count], |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | count * sizeof(xfs_dir2_leaf_entry_t)); |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame] | 949 | xfs_dir3_leaf_log_ents(args, bp_s, start_s, start_s + count - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | } |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 951 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | /* |
| 953 | * Update the headers and log them. |
| 954 | */ |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 955 | shdr->count -= count; |
| 956 | shdr->stale -= stale; |
| 957 | dhdr->count += count; |
| 958 | dhdr->stale += stale; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 959 | } |
| 960 | |
| 961 | /* |
| 962 | * Determine the sort order of two leaf blocks. |
| 963 | * Returns 1 if both are valid and leaf2 should be before leaf1, else 0. |
| 964 | */ |
| 965 | int /* sort order */ |
| 966 | xfs_dir2_leafn_order( |
Dave Chinner | 4141956 | 2013-10-29 22:11:50 +1100 | [diff] [blame] | 967 | struct xfs_inode *dp, |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 968 | struct xfs_buf *leaf1_bp, /* leaf1 buffer */ |
| 969 | struct xfs_buf *leaf2_bp) /* leaf2 buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | { |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 971 | struct xfs_dir2_leaf *leaf1 = leaf1_bp->b_addr; |
| 972 | struct xfs_dir2_leaf *leaf2 = leaf2_bp->b_addr; |
| 973 | struct xfs_dir2_leaf_entry *ents1; |
| 974 | struct xfs_dir2_leaf_entry *ents2; |
| 975 | struct xfs_dir3_icleaf_hdr hdr1; |
| 976 | struct xfs_dir3_icleaf_hdr hdr2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 977 | |
Dave Chinner | 01ba43b | 2013-10-29 22:11:52 +1100 | [diff] [blame] | 978 | dp->d_ops->leaf_hdr_from_disk(&hdr1, leaf1); |
| 979 | dp->d_ops->leaf_hdr_from_disk(&hdr2, leaf2); |
Dave Chinner | 4141956 | 2013-10-29 22:11:50 +1100 | [diff] [blame] | 980 | ents1 = dp->d_ops->leaf_ents_p(leaf1); |
| 981 | ents2 = dp->d_ops->leaf_ents_p(leaf2); |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 982 | |
| 983 | if (hdr1.count > 0 && hdr2.count > 0 && |
| 984 | (be32_to_cpu(ents2[0].hashval) < be32_to_cpu(ents1[0].hashval) || |
| 985 | be32_to_cpu(ents2[hdr2.count - 1].hashval) < |
| 986 | be32_to_cpu(ents1[hdr1.count - 1].hashval))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 987 | return 1; |
| 988 | return 0; |
| 989 | } |
| 990 | |
| 991 | /* |
| 992 | * Rebalance leaf entries between two leaf blocks. |
| 993 | * This is actually only called when the second block is new, |
| 994 | * though the code deals with the general case. |
| 995 | * A new entry will be inserted in one of the blocks, and that |
| 996 | * entry is taken into account when balancing. |
| 997 | */ |
| 998 | static void |
| 999 | xfs_dir2_leafn_rebalance( |
| 1000 | xfs_da_state_t *state, /* btree cursor */ |
| 1001 | xfs_da_state_blk_t *blk1, /* first btree block */ |
| 1002 | xfs_da_state_blk_t *blk2) /* second btree block */ |
| 1003 | { |
| 1004 | xfs_da_args_t *args; /* operation arguments */ |
| 1005 | int count; /* count (& direction) leaves */ |
| 1006 | int isleft; /* new goes in left leaf */ |
| 1007 | xfs_dir2_leaf_t *leaf1; /* first leaf structure */ |
| 1008 | xfs_dir2_leaf_t *leaf2; /* second leaf structure */ |
| 1009 | int mid; /* midpoint leaf index */ |
Dave Chinner | 742ae1e | 2013-04-30 21:39:34 +1000 | [diff] [blame] | 1010 | #if defined(DEBUG) || defined(XFS_WARN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1011 | int oldstale; /* old count of stale leaves */ |
| 1012 | #endif |
| 1013 | int oldsum; /* old total leaf count */ |
Gustavo A. R. Silva | e4e542a | 2018-07-17 14:25:01 -0700 | [diff] [blame] | 1014 | int swap_blocks; /* swapped leaf blocks */ |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1015 | struct xfs_dir2_leaf_entry *ents1; |
| 1016 | struct xfs_dir2_leaf_entry *ents2; |
| 1017 | struct xfs_dir3_icleaf_hdr hdr1; |
| 1018 | struct xfs_dir3_icleaf_hdr hdr2; |
Dave Chinner | 4141956 | 2013-10-29 22:11:50 +1100 | [diff] [blame] | 1019 | struct xfs_inode *dp = state->args->dp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | |
| 1021 | args = state->args; |
| 1022 | /* |
| 1023 | * If the block order is wrong, swap the arguments. |
| 1024 | */ |
Gustavo A. R. Silva | e4e542a | 2018-07-17 14:25:01 -0700 | [diff] [blame] | 1025 | swap_blocks = xfs_dir2_leafn_order(dp, blk1->bp, blk2->bp); |
| 1026 | if (swap_blocks) |
| 1027 | swap(blk1, blk2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1029 | leaf1 = blk1->bp->b_addr; |
| 1030 | leaf2 = blk2->bp->b_addr; |
Dave Chinner | 01ba43b | 2013-10-29 22:11:52 +1100 | [diff] [blame] | 1031 | dp->d_ops->leaf_hdr_from_disk(&hdr1, leaf1); |
| 1032 | dp->d_ops->leaf_hdr_from_disk(&hdr2, leaf2); |
Dave Chinner | 4141956 | 2013-10-29 22:11:50 +1100 | [diff] [blame] | 1033 | ents1 = dp->d_ops->leaf_ents_p(leaf1); |
| 1034 | ents2 = dp->d_ops->leaf_ents_p(leaf2); |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1035 | |
| 1036 | oldsum = hdr1.count + hdr2.count; |
Dave Chinner | 742ae1e | 2013-04-30 21:39:34 +1000 | [diff] [blame] | 1037 | #if defined(DEBUG) || defined(XFS_WARN) |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1038 | oldstale = hdr1.stale + hdr2.stale; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1039 | #endif |
| 1040 | mid = oldsum >> 1; |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1041 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | /* |
| 1043 | * If the old leaf count was odd then the new one will be even, |
| 1044 | * so we need to divide the new count evenly. |
| 1045 | */ |
| 1046 | if (oldsum & 1) { |
| 1047 | xfs_dahash_t midhash; /* middle entry hash value */ |
| 1048 | |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1049 | if (mid >= hdr1.count) |
| 1050 | midhash = be32_to_cpu(ents2[mid - hdr1.count].hashval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1051 | else |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1052 | midhash = be32_to_cpu(ents1[mid].hashval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1053 | isleft = args->hashval <= midhash; |
| 1054 | } |
| 1055 | /* |
| 1056 | * If the old count is even then the new count is odd, so there's |
| 1057 | * no preferred side for the new entry. |
| 1058 | * Pick the left one. |
| 1059 | */ |
| 1060 | else |
| 1061 | isleft = 1; |
| 1062 | /* |
| 1063 | * Calculate moved entry count. Positive means left-to-right, |
| 1064 | * negative means right-to-left. Then move the entries. |
| 1065 | */ |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1066 | count = hdr1.count - mid + (isleft == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | if (count > 0) |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1068 | xfs_dir3_leafn_moveents(args, blk1->bp, &hdr1, ents1, |
| 1069 | hdr1.count - count, blk2->bp, |
| 1070 | &hdr2, ents2, 0, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1071 | else if (count < 0) |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1072 | xfs_dir3_leafn_moveents(args, blk2->bp, &hdr2, ents2, 0, |
| 1073 | blk1->bp, &hdr1, ents1, |
| 1074 | hdr1.count, count); |
| 1075 | |
| 1076 | ASSERT(hdr1.count + hdr2.count == oldsum); |
| 1077 | ASSERT(hdr1.stale + hdr2.stale == oldstale); |
| 1078 | |
| 1079 | /* log the changes made when moving the entries */ |
Dave Chinner | 01ba43b | 2013-10-29 22:11:52 +1100 | [diff] [blame] | 1080 | dp->d_ops->leaf_hdr_to_disk(leaf1, &hdr1); |
| 1081 | dp->d_ops->leaf_hdr_to_disk(leaf2, &hdr2); |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame] | 1082 | xfs_dir3_leaf_log_header(args, blk1->bp); |
| 1083 | xfs_dir3_leaf_log_header(args, blk2->bp); |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1084 | |
Dave Chinner | 4141956 | 2013-10-29 22:11:50 +1100 | [diff] [blame] | 1085 | xfs_dir3_leaf_check(dp, blk1->bp); |
| 1086 | xfs_dir3_leaf_check(dp, blk2->bp); |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1087 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1088 | /* |
| 1089 | * Mark whether we're inserting into the old or new leaf. |
| 1090 | */ |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1091 | if (hdr1.count < hdr2.count) |
Gustavo A. R. Silva | e4e542a | 2018-07-17 14:25:01 -0700 | [diff] [blame] | 1092 | state->inleaf = swap_blocks; |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1093 | else if (hdr1.count > hdr2.count) |
Gustavo A. R. Silva | e4e542a | 2018-07-17 14:25:01 -0700 | [diff] [blame] | 1094 | state->inleaf = !swap_blocks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1095 | else |
Gustavo A. R. Silva | e4e542a | 2018-07-17 14:25:01 -0700 | [diff] [blame] | 1096 | state->inleaf = swap_blocks ^ (blk1->index <= hdr1.count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1097 | /* |
| 1098 | * Adjust the expected index for insertion. |
| 1099 | */ |
| 1100 | if (!state->inleaf) |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1101 | blk2->index = blk1->index - hdr1.count; |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame] | 1102 | |
| 1103 | /* |
| 1104 | * Finally sanity check just to make sure we are not returning a |
| 1105 | * negative index |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1106 | */ |
Dave Chinner | 4141956 | 2013-10-29 22:11:50 +1100 | [diff] [blame] | 1107 | if (blk2->index < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1108 | state->inleaf = 1; |
| 1109 | blk2->index = 0; |
Dave Chinner | 4141956 | 2013-10-29 22:11:50 +1100 | [diff] [blame] | 1110 | xfs_alert(dp->i_mount, |
Eric Sandeen | 08e96e1 | 2013-10-11 20:59:05 -0500 | [diff] [blame] | 1111 | "%s: picked the wrong leaf? reverting original leaf: blk1->index %d", |
Dave Chinner | 0b932cc | 2011-03-07 10:08:35 +1100 | [diff] [blame] | 1112 | __func__, blk1->index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1113 | } |
| 1114 | } |
| 1115 | |
Dave Chinner | 2025207 | 2012-11-12 22:54:13 +1100 | [diff] [blame] | 1116 | static int |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 1117 | xfs_dir3_data_block_free( |
Dave Chinner | 2025207 | 2012-11-12 22:54:13 +1100 | [diff] [blame] | 1118 | xfs_da_args_t *args, |
| 1119 | struct xfs_dir2_data_hdr *hdr, |
| 1120 | struct xfs_dir2_free *free, |
| 1121 | xfs_dir2_db_t fdb, |
| 1122 | int findex, |
| 1123 | struct xfs_buf *fbp, |
| 1124 | int longest) |
| 1125 | { |
Dave Chinner | 2025207 | 2012-11-12 22:54:13 +1100 | [diff] [blame] | 1126 | int logfree = 0; |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 1127 | __be16 *bests; |
| 1128 | struct xfs_dir3_icfree_hdr freehdr; |
Dave Chinner | 01ba43b | 2013-10-29 22:11:52 +1100 | [diff] [blame] | 1129 | struct xfs_inode *dp = args->dp; |
Dave Chinner | 2025207 | 2012-11-12 22:54:13 +1100 | [diff] [blame] | 1130 | |
Dave Chinner | 01ba43b | 2013-10-29 22:11:52 +1100 | [diff] [blame] | 1131 | dp->d_ops->free_hdr_from_disk(&freehdr, free); |
Dave Chinner | 24dd0f5 | 2013-10-30 13:48:41 -0500 | [diff] [blame] | 1132 | bests = dp->d_ops->free_bests_p(free); |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 1133 | if (hdr) { |
Dave Chinner | 2025207 | 2012-11-12 22:54:13 +1100 | [diff] [blame] | 1134 | /* |
| 1135 | * Data block is not empty, just set the free entry to the new |
| 1136 | * value. |
| 1137 | */ |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 1138 | bests[findex] = cpu_to_be16(longest); |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame] | 1139 | xfs_dir2_free_log_bests(args, fbp, findex, findex); |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 1140 | return 0; |
| 1141 | } |
| 1142 | |
| 1143 | /* One less used entry in the free table. */ |
| 1144 | freehdr.nused--; |
| 1145 | |
| 1146 | /* |
| 1147 | * If this was the last entry in the table, we can trim the table size |
| 1148 | * back. There might be other entries at the end referring to |
| 1149 | * non-existent data blocks, get those too. |
| 1150 | */ |
| 1151 | if (findex == freehdr.nvalid - 1) { |
| 1152 | int i; /* free entry index */ |
| 1153 | |
| 1154 | for (i = findex - 1; i >= 0; i--) { |
| 1155 | if (bests[i] != cpu_to_be16(NULLDATAOFF)) |
| 1156 | break; |
| 1157 | } |
| 1158 | freehdr.nvalid = i + 1; |
| 1159 | logfree = 0; |
| 1160 | } else { |
| 1161 | /* Not the last entry, just punch it out. */ |
| 1162 | bests[findex] = cpu_to_be16(NULLDATAOFF); |
Dave Chinner | 2025207 | 2012-11-12 22:54:13 +1100 | [diff] [blame] | 1163 | logfree = 1; |
| 1164 | } |
| 1165 | |
Dave Chinner | 01ba43b | 2013-10-29 22:11:52 +1100 | [diff] [blame] | 1166 | dp->d_ops->free_hdr_to_disk(free, &freehdr); |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame] | 1167 | xfs_dir2_free_log_header(args, fbp); |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 1168 | |
| 1169 | /* |
| 1170 | * If there are no useful entries left in the block, get rid of the |
| 1171 | * block if we can. |
| 1172 | */ |
| 1173 | if (!freehdr.nused) { |
| 1174 | int error; |
| 1175 | |
| 1176 | error = xfs_dir2_shrink_inode(args, fdb, fbp); |
| 1177 | if (error == 0) { |
| 1178 | fbp = NULL; |
| 1179 | logfree = 0; |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1180 | } else if (error != -ENOSPC || args->total != 0) |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 1181 | return error; |
| 1182 | /* |
| 1183 | * It's possible to get ENOSPC if there is no |
| 1184 | * space reservation. In this case some one |
| 1185 | * else will eventually get rid of this block. |
| 1186 | */ |
| 1187 | } |
| 1188 | |
Dave Chinner | 2025207 | 2012-11-12 22:54:13 +1100 | [diff] [blame] | 1189 | /* Log the free entry that changed, unless we got rid of it. */ |
| 1190 | if (logfree) |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame] | 1191 | xfs_dir2_free_log_bests(args, fbp, findex, findex); |
Dave Chinner | 2025207 | 2012-11-12 22:54:13 +1100 | [diff] [blame] | 1192 | return 0; |
| 1193 | } |
| 1194 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1195 | /* |
| 1196 | * Remove an entry from a node directory. |
| 1197 | * This removes the leaf entry and the data entry, |
| 1198 | * and updates the free block if necessary. |
| 1199 | */ |
| 1200 | static int /* error */ |
| 1201 | xfs_dir2_leafn_remove( |
| 1202 | xfs_da_args_t *args, /* operation arguments */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1203 | struct xfs_buf *bp, /* leaf buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1204 | int index, /* leaf entry index */ |
| 1205 | xfs_da_state_blk_t *dblk, /* data block */ |
| 1206 | int *rval) /* resulting block needs join */ |
| 1207 | { |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 1208 | xfs_dir2_data_hdr_t *hdr; /* data block header */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1209 | xfs_dir2_db_t db; /* data block number */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1210 | struct xfs_buf *dbp; /* data block buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1211 | xfs_dir2_data_entry_t *dep; /* data block entry */ |
| 1212 | xfs_inode_t *dp; /* incore directory inode */ |
| 1213 | xfs_dir2_leaf_t *leaf; /* leaf structure */ |
| 1214 | xfs_dir2_leaf_entry_t *lep; /* leaf entry */ |
| 1215 | int longest; /* longest data free entry */ |
| 1216 | int off; /* data block entry offset */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1217 | int needlog; /* need to log data header */ |
| 1218 | int needscan; /* need to rescan data frees */ |
| 1219 | xfs_trans_t *tp; /* transaction pointer */ |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame] | 1220 | struct xfs_dir2_data_free *bf; /* bestfree table */ |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1221 | struct xfs_dir3_icleaf_hdr leafhdr; |
| 1222 | struct xfs_dir2_leaf_entry *ents; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1223 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1224 | trace_xfs_dir2_leafn_remove(args, index); |
| 1225 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1226 | dp = args->dp; |
| 1227 | tp = args->trans; |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1228 | leaf = bp->b_addr; |
Dave Chinner | 01ba43b | 2013-10-29 22:11:52 +1100 | [diff] [blame] | 1229 | dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf); |
Dave Chinner | 4141956 | 2013-10-29 22:11:50 +1100 | [diff] [blame] | 1230 | ents = dp->d_ops->leaf_ents_p(leaf); |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1231 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1232 | /* |
| 1233 | * Point to the entry we're removing. |
| 1234 | */ |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1235 | lep = &ents[index]; |
| 1236 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1237 | /* |
| 1238 | * Extract the data block and offset from the entry. |
| 1239 | */ |
Dave Chinner | 3002803 | 2014-06-06 15:08:18 +1000 | [diff] [blame] | 1240 | db = xfs_dir2_dataptr_to_db(args->geo, be32_to_cpu(lep->address)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1241 | ASSERT(dblk->blkno == db); |
Dave Chinner | 3002803 | 2014-06-06 15:08:18 +1000 | [diff] [blame] | 1242 | off = xfs_dir2_dataptr_to_off(args->geo, be32_to_cpu(lep->address)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1243 | ASSERT(dblk->index == off); |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1244 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1245 | /* |
| 1246 | * Kill the leaf entry by marking it stale. |
| 1247 | * Log the leaf block changes. |
| 1248 | */ |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1249 | leafhdr.stale++; |
Dave Chinner | 01ba43b | 2013-10-29 22:11:52 +1100 | [diff] [blame] | 1250 | dp->d_ops->leaf_hdr_to_disk(leaf, &leafhdr); |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame] | 1251 | xfs_dir3_leaf_log_header(args, bp); |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1252 | |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 1253 | lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR); |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame] | 1254 | xfs_dir3_leaf_log_ents(args, bp, index, index); |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1255 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1256 | /* |
| 1257 | * Make the data entry free. Keep track of the longest freespace |
| 1258 | * in the data block in case it changes. |
| 1259 | */ |
| 1260 | dbp = dblk->bp; |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1261 | hdr = dbp->b_addr; |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 1262 | dep = (xfs_dir2_data_entry_t *)((char *)hdr + off); |
Dave Chinner | 2ca9877 | 2013-10-29 22:11:49 +1100 | [diff] [blame] | 1263 | bf = dp->d_ops->data_bestfree_p(hdr); |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame] | 1264 | longest = be16_to_cpu(bf[0].length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1265 | needlog = needscan = 0; |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame] | 1266 | xfs_dir2_data_make_free(args, dbp, off, |
Dave Chinner | 9d23fc8 | 2013-10-29 22:11:48 +1100 | [diff] [blame] | 1267 | dp->d_ops->data_entsize(dep->namelen), &needlog, &needscan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1268 | /* |
| 1269 | * Rescan the data block freespaces for bestfree. |
| 1270 | * Log the data block header if needed. |
| 1271 | */ |
| 1272 | if (needscan) |
Dave Chinner | 9d23fc8 | 2013-10-29 22:11:48 +1100 | [diff] [blame] | 1273 | xfs_dir2_data_freescan(dp, hdr, &needlog); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1274 | if (needlog) |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame] | 1275 | xfs_dir2_data_log_header(args, dbp); |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame] | 1276 | xfs_dir3_data_check(dp, dbp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1277 | /* |
| 1278 | * If the longest data block freespace changes, need to update |
| 1279 | * the corresponding freeblock entry. |
| 1280 | */ |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame] | 1281 | if (longest < be16_to_cpu(bf[0].length)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1282 | int error; /* error return value */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1283 | struct xfs_buf *fbp; /* freeblock buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1284 | xfs_dir2_db_t fdb; /* freeblock block number */ |
| 1285 | int findex; /* index in freeblock entries */ |
| 1286 | xfs_dir2_free_t *free; /* freeblock structure */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1287 | |
| 1288 | /* |
| 1289 | * Convert the data block number to a free block, |
| 1290 | * read in the free block. |
| 1291 | */ |
Dave Chinner | 8f66193 | 2014-06-06 15:15:59 +1000 | [diff] [blame] | 1292 | fdb = dp->d_ops->db_to_fdb(args->geo, db); |
Dave Chinner | 2998ab1d | 2014-06-06 15:07:53 +1000 | [diff] [blame] | 1293 | error = xfs_dir2_free_read(tp, dp, |
| 1294 | xfs_dir2_db_to_da(args->geo, fdb), |
Dave Chinner | 2025207 | 2012-11-12 22:54:13 +1100 | [diff] [blame] | 1295 | &fbp); |
Dave Chinner | 4bb20a8 | 2012-11-12 22:54:10 +1100 | [diff] [blame] | 1296 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1297 | return error; |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1298 | free = fbp->b_addr; |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 1299 | #ifdef DEBUG |
| 1300 | { |
| 1301 | struct xfs_dir3_icfree_hdr freehdr; |
Dave Chinner | 01ba43b | 2013-10-29 22:11:52 +1100 | [diff] [blame] | 1302 | dp->d_ops->free_hdr_from_disk(&freehdr, free); |
Dave Chinner | 8f66193 | 2014-06-06 15:15:59 +1000 | [diff] [blame] | 1303 | ASSERT(freehdr.firstdb == dp->d_ops->free_max_bests(args->geo) * |
Dave Chinner | 3002803 | 2014-06-06 15:08:18 +1000 | [diff] [blame] | 1304 | (fdb - xfs_dir2_byte_to_db(args->geo, |
| 1305 | XFS_DIR2_FREE_OFFSET))); |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 1306 | } |
| 1307 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1308 | /* |
| 1309 | * Calculate which entry we need to fix. |
| 1310 | */ |
Dave Chinner | 8f66193 | 2014-06-06 15:15:59 +1000 | [diff] [blame] | 1311 | findex = dp->d_ops->db_to_fdindex(args->geo, db); |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame] | 1312 | longest = be16_to_cpu(bf[0].length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1313 | /* |
| 1314 | * If the data block is now empty we can get rid of it |
| 1315 | * (usually). |
| 1316 | */ |
Dave Chinner | 8f66193 | 2014-06-06 15:15:59 +1000 | [diff] [blame] | 1317 | if (longest == args->geo->blksize - |
Dave Chinner | 1c9a5b2 | 2013-10-30 09:15:02 +1100 | [diff] [blame] | 1318 | dp->d_ops->data_entry_offset) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1319 | /* |
| 1320 | * Try to punch out the data block. |
| 1321 | */ |
| 1322 | error = xfs_dir2_shrink_inode(args, db, dbp); |
| 1323 | if (error == 0) { |
| 1324 | dblk->bp = NULL; |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 1325 | hdr = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1326 | } |
| 1327 | /* |
| 1328 | * We can get ENOSPC if there's no space reservation. |
| 1329 | * In this case just drop the buffer and some one else |
| 1330 | * will eventually get rid of the empty block. |
| 1331 | */ |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1332 | else if (!(error == -ENOSPC && args->total == 0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1333 | return error; |
| 1334 | } |
| 1335 | /* |
| 1336 | * If we got rid of the data block, we can eliminate that entry |
| 1337 | * in the free block. |
| 1338 | */ |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 1339 | error = xfs_dir3_data_block_free(args, hdr, free, |
Dave Chinner | 2025207 | 2012-11-12 22:54:13 +1100 | [diff] [blame] | 1340 | fdb, findex, fbp, longest); |
| 1341 | if (error) |
| 1342 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1343 | } |
Dave Chinner | 2025207 | 2012-11-12 22:54:13 +1100 | [diff] [blame] | 1344 | |
Dave Chinner | 4141956 | 2013-10-29 22:11:50 +1100 | [diff] [blame] | 1345 | xfs_dir3_leaf_check(dp, bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1346 | /* |
Malcolm Parsons | 9da096f | 2009-03-29 09:55:42 +0200 | [diff] [blame] | 1347 | * Return indication of whether this leaf block is empty enough |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1348 | * to justify trying to join it with a neighbor. |
| 1349 | */ |
Dave Chinner | 1c9a5b2 | 2013-10-30 09:15:02 +1100 | [diff] [blame] | 1350 | *rval = (dp->d_ops->leaf_hdr_size + |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1351 | (uint)sizeof(ents[0]) * (leafhdr.count - leafhdr.stale)) < |
Dave Chinner | ed358c0 | 2014-06-06 15:18:10 +1000 | [diff] [blame] | 1352 | args->geo->magicpct; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1353 | return 0; |
| 1354 | } |
| 1355 | |
| 1356 | /* |
| 1357 | * Split the leaf entries in the old block into old and new blocks. |
| 1358 | */ |
| 1359 | int /* error */ |
| 1360 | xfs_dir2_leafn_split( |
| 1361 | xfs_da_state_t *state, /* btree cursor */ |
| 1362 | xfs_da_state_blk_t *oldblk, /* original block */ |
| 1363 | xfs_da_state_blk_t *newblk) /* newly created block */ |
| 1364 | { |
| 1365 | xfs_da_args_t *args; /* operation arguments */ |
| 1366 | xfs_dablk_t blkno; /* new leaf block number */ |
| 1367 | int error; /* error return value */ |
Dave Chinner | 4141956 | 2013-10-29 22:11:50 +1100 | [diff] [blame] | 1368 | struct xfs_inode *dp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1369 | |
| 1370 | /* |
| 1371 | * Allocate space for a new leaf node. |
| 1372 | */ |
| 1373 | args = state->args; |
Dave Chinner | 4141956 | 2013-10-29 22:11:50 +1100 | [diff] [blame] | 1374 | dp = args->dp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1375 | ASSERT(oldblk->magic == XFS_DIR2_LEAFN_MAGIC); |
| 1376 | error = xfs_da_grow_inode(args, &blkno); |
| 1377 | if (error) { |
| 1378 | return error; |
| 1379 | } |
| 1380 | /* |
| 1381 | * Initialize the new leaf block. |
| 1382 | */ |
Dave Chinner | 2998ab1d | 2014-06-06 15:07:53 +1000 | [diff] [blame] | 1383 | error = xfs_dir3_leaf_get_buf(args, xfs_dir2_da_to_db(args->geo, blkno), |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1384 | &newblk->bp, XFS_DIR2_LEAFN_MAGIC); |
| 1385 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1386 | return error; |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1387 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1388 | newblk->blkno = blkno; |
| 1389 | newblk->magic = XFS_DIR2_LEAFN_MAGIC; |
| 1390 | /* |
| 1391 | * Rebalance the entries across the two leaves, link the new |
| 1392 | * block into the leaves. |
| 1393 | */ |
| 1394 | xfs_dir2_leafn_rebalance(state, oldblk, newblk); |
Dave Chinner | f5ea110 | 2013-04-24 18:58:02 +1000 | [diff] [blame] | 1395 | error = xfs_da3_blk_link(state, oldblk, newblk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1396 | if (error) { |
| 1397 | return error; |
| 1398 | } |
| 1399 | /* |
| 1400 | * Insert the new entry in the correct block. |
| 1401 | */ |
| 1402 | if (state->inleaf) |
| 1403 | error = xfs_dir2_leafn_add(oldblk->bp, args, oldblk->index); |
| 1404 | else |
| 1405 | error = xfs_dir2_leafn_add(newblk->bp, args, newblk->index); |
| 1406 | /* |
| 1407 | * Update last hashval in each block since we added the name. |
| 1408 | */ |
Darrick J. Wong | 8e8877e | 2017-06-16 11:00:13 -0700 | [diff] [blame] | 1409 | oldblk->hashval = xfs_dir2_leaf_lasthash(dp, oldblk->bp, NULL); |
| 1410 | newblk->hashval = xfs_dir2_leaf_lasthash(dp, newblk->bp, NULL); |
Dave Chinner | 4141956 | 2013-10-29 22:11:50 +1100 | [diff] [blame] | 1411 | xfs_dir3_leaf_check(dp, oldblk->bp); |
| 1412 | xfs_dir3_leaf_check(dp, newblk->bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1413 | return error; |
| 1414 | } |
| 1415 | |
| 1416 | /* |
| 1417 | * Check a leaf block and its neighbors to see if the block should be |
| 1418 | * collapsed into one or the other neighbor. Always keep the block |
| 1419 | * with the smaller block number. |
| 1420 | * If the current block is over 50% full, don't try to join it, return 0. |
| 1421 | * If the block is empty, fill in the state structure and return 2. |
| 1422 | * If it can be collapsed, fill in the state structure and return 1. |
| 1423 | * If nothing can be done, return 0. |
| 1424 | */ |
| 1425 | int /* error */ |
| 1426 | xfs_dir2_leafn_toosmall( |
| 1427 | xfs_da_state_t *state, /* btree cursor */ |
| 1428 | int *action) /* resulting action to take */ |
| 1429 | { |
| 1430 | xfs_da_state_blk_t *blk; /* leaf block */ |
| 1431 | xfs_dablk_t blkno; /* leaf block number */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1432 | struct xfs_buf *bp; /* leaf buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1433 | int bytes; /* bytes in use */ |
| 1434 | int count; /* leaf live entry count */ |
| 1435 | int error; /* error return value */ |
| 1436 | int forward; /* sibling block direction */ |
| 1437 | int i; /* sibling counter */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1438 | xfs_dir2_leaf_t *leaf; /* leaf structure */ |
| 1439 | int rval; /* result from path_shift */ |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1440 | struct xfs_dir3_icleaf_hdr leafhdr; |
| 1441 | struct xfs_dir2_leaf_entry *ents; |
Dave Chinner | 4141956 | 2013-10-29 22:11:50 +1100 | [diff] [blame] | 1442 | struct xfs_inode *dp = state->args->dp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1443 | |
| 1444 | /* |
| 1445 | * Check for the degenerate case of the block being over 50% full. |
| 1446 | * If so, it's not worth even looking to see if we might be able |
| 1447 | * to coalesce with a sibling. |
| 1448 | */ |
| 1449 | blk = &state->path.blk[state->path.active - 1]; |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1450 | leaf = blk->bp->b_addr; |
Dave Chinner | 01ba43b | 2013-10-29 22:11:52 +1100 | [diff] [blame] | 1451 | dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf); |
Dave Chinner | 4141956 | 2013-10-29 22:11:50 +1100 | [diff] [blame] | 1452 | ents = dp->d_ops->leaf_ents_p(leaf); |
| 1453 | xfs_dir3_leaf_check(dp, blk->bp); |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1454 | |
| 1455 | count = leafhdr.count - leafhdr.stale; |
Dave Chinner | 1c9a5b2 | 2013-10-30 09:15:02 +1100 | [diff] [blame] | 1456 | bytes = dp->d_ops->leaf_hdr_size + count * sizeof(ents[0]); |
Dave Chinner | b2a21e7 | 2014-06-06 15:22:04 +1000 | [diff] [blame] | 1457 | if (bytes > (state->args->geo->blksize >> 1)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1458 | /* |
| 1459 | * Blk over 50%, don't try to join. |
| 1460 | */ |
| 1461 | *action = 0; |
| 1462 | return 0; |
| 1463 | } |
| 1464 | /* |
| 1465 | * Check for the degenerate case of the block being empty. |
| 1466 | * If the block is empty, we'll simply delete it, no need to |
| 1467 | * coalesce it with a sibling block. We choose (arbitrarily) |
| 1468 | * to merge with the forward block unless it is NULL. |
| 1469 | */ |
| 1470 | if (count == 0) { |
| 1471 | /* |
| 1472 | * Make altpath point to the block we want to keep and |
| 1473 | * path point to the block we want to drop (this one). |
| 1474 | */ |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1475 | forward = (leafhdr.forw != 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1476 | memcpy(&state->altpath, &state->path, sizeof(state->path)); |
Dave Chinner | f5ea110 | 2013-04-24 18:58:02 +1000 | [diff] [blame] | 1477 | error = xfs_da3_path_shift(state, &state->altpath, forward, 0, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1478 | &rval); |
| 1479 | if (error) |
| 1480 | return error; |
| 1481 | *action = rval ? 2 : 0; |
| 1482 | return 0; |
| 1483 | } |
| 1484 | /* |
| 1485 | * Examine each sibling block to see if we can coalesce with |
| 1486 | * at least 25% free space to spare. We need to figure out |
| 1487 | * whether to merge with the forward or the backward block. |
| 1488 | * We prefer coalescing with the lower numbered sibling so as |
| 1489 | * to shrink a directory over time. |
| 1490 | */ |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1491 | forward = leafhdr.forw < leafhdr.back; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1492 | for (i = 0, bp = NULL; i < 2; forward = !forward, i++) { |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1493 | struct xfs_dir3_icleaf_hdr hdr2; |
| 1494 | |
| 1495 | blkno = forward ? leafhdr.forw : leafhdr.back; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1496 | if (blkno == 0) |
| 1497 | continue; |
| 1498 | /* |
| 1499 | * Read the sibling leaf block. |
| 1500 | */ |
Dave Chinner | 4141956 | 2013-10-29 22:11:50 +1100 | [diff] [blame] | 1501 | error = xfs_dir3_leafn_read(state->args->trans, dp, |
Dave Chinner | e6f7667 | 2012-11-12 22:54:15 +1100 | [diff] [blame] | 1502 | blkno, -1, &bp); |
Dave Chinner | 4bb20a8 | 2012-11-12 22:54:10 +1100 | [diff] [blame] | 1503 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1504 | return error; |
Dave Chinner | e6f7667 | 2012-11-12 22:54:15 +1100 | [diff] [blame] | 1505 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1506 | /* |
| 1507 | * Count bytes in the two blocks combined. |
| 1508 | */ |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1509 | count = leafhdr.count - leafhdr.stale; |
Dave Chinner | b2a21e7 | 2014-06-06 15:22:04 +1000 | [diff] [blame] | 1510 | bytes = state->args->geo->blksize - |
| 1511 | (state->args->geo->blksize >> 2); |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1512 | |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1513 | leaf = bp->b_addr; |
Dave Chinner | 01ba43b | 2013-10-29 22:11:52 +1100 | [diff] [blame] | 1514 | dp->d_ops->leaf_hdr_from_disk(&hdr2, leaf); |
Dave Chinner | 4141956 | 2013-10-29 22:11:50 +1100 | [diff] [blame] | 1515 | ents = dp->d_ops->leaf_ents_p(leaf); |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1516 | count += hdr2.count - hdr2.stale; |
| 1517 | bytes -= count * sizeof(ents[0]); |
| 1518 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1519 | /* |
| 1520 | * Fits with at least 25% to spare. |
| 1521 | */ |
| 1522 | if (bytes >= 0) |
| 1523 | break; |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1524 | xfs_trans_brelse(state->args->trans, bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1525 | } |
| 1526 | /* |
| 1527 | * Didn't like either block, give up. |
| 1528 | */ |
| 1529 | if (i >= 2) { |
| 1530 | *action = 0; |
| 1531 | return 0; |
| 1532 | } |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1533 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1534 | /* |
| 1535 | * Make altpath point to the block we want to keep (the lower |
| 1536 | * numbered block) and path point to the block we want to drop. |
| 1537 | */ |
| 1538 | memcpy(&state->altpath, &state->path, sizeof(state->path)); |
| 1539 | if (blkno < blk->blkno) |
Dave Chinner | f5ea110 | 2013-04-24 18:58:02 +1000 | [diff] [blame] | 1540 | error = xfs_da3_path_shift(state, &state->altpath, forward, 0, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1541 | &rval); |
| 1542 | else |
Dave Chinner | f5ea110 | 2013-04-24 18:58:02 +1000 | [diff] [blame] | 1543 | error = xfs_da3_path_shift(state, &state->path, forward, 0, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1544 | &rval); |
| 1545 | if (error) { |
| 1546 | return error; |
| 1547 | } |
| 1548 | *action = rval ? 0 : 1; |
| 1549 | return 0; |
| 1550 | } |
| 1551 | |
| 1552 | /* |
| 1553 | * Move all the leaf entries from drop_blk to save_blk. |
| 1554 | * This is done as part of a join operation. |
| 1555 | */ |
| 1556 | void |
| 1557 | xfs_dir2_leafn_unbalance( |
| 1558 | xfs_da_state_t *state, /* cursor */ |
| 1559 | xfs_da_state_blk_t *drop_blk, /* dead block */ |
| 1560 | xfs_da_state_blk_t *save_blk) /* surviving block */ |
| 1561 | { |
| 1562 | xfs_da_args_t *args; /* operation arguments */ |
| 1563 | xfs_dir2_leaf_t *drop_leaf; /* dead leaf structure */ |
| 1564 | xfs_dir2_leaf_t *save_leaf; /* surviving leaf structure */ |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1565 | struct xfs_dir3_icleaf_hdr savehdr; |
| 1566 | struct xfs_dir3_icleaf_hdr drophdr; |
| 1567 | struct xfs_dir2_leaf_entry *sents; |
| 1568 | struct xfs_dir2_leaf_entry *dents; |
Dave Chinner | 4141956 | 2013-10-29 22:11:50 +1100 | [diff] [blame] | 1569 | struct xfs_inode *dp = state->args->dp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1570 | |
| 1571 | args = state->args; |
| 1572 | ASSERT(drop_blk->magic == XFS_DIR2_LEAFN_MAGIC); |
| 1573 | ASSERT(save_blk->magic == XFS_DIR2_LEAFN_MAGIC); |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1574 | drop_leaf = drop_blk->bp->b_addr; |
| 1575 | save_leaf = save_blk->bp->b_addr; |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1576 | |
Dave Chinner | 01ba43b | 2013-10-29 22:11:52 +1100 | [diff] [blame] | 1577 | dp->d_ops->leaf_hdr_from_disk(&savehdr, save_leaf); |
| 1578 | dp->d_ops->leaf_hdr_from_disk(&drophdr, drop_leaf); |
| 1579 | sents = dp->d_ops->leaf_ents_p(save_leaf); |
| 1580 | dents = dp->d_ops->leaf_ents_p(drop_leaf); |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1581 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1582 | /* |
| 1583 | * If there are any stale leaf entries, take this opportunity |
| 1584 | * to purge them. |
| 1585 | */ |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1586 | if (drophdr.stale) |
| 1587 | xfs_dir3_leaf_compact(args, &drophdr, drop_blk->bp); |
| 1588 | if (savehdr.stale) |
| 1589 | xfs_dir3_leaf_compact(args, &savehdr, save_blk->bp); |
| 1590 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1591 | /* |
| 1592 | * Move the entries from drop to the appropriate end of save. |
| 1593 | */ |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1594 | drop_blk->hashval = be32_to_cpu(dents[drophdr.count - 1].hashval); |
Dave Chinner | 4141956 | 2013-10-29 22:11:50 +1100 | [diff] [blame] | 1595 | if (xfs_dir2_leafn_order(dp, save_blk->bp, drop_blk->bp)) |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1596 | xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0, |
| 1597 | save_blk->bp, &savehdr, sents, 0, |
| 1598 | drophdr.count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1599 | else |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1600 | xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0, |
| 1601 | save_blk->bp, &savehdr, sents, |
| 1602 | savehdr.count, drophdr.count); |
| 1603 | save_blk->hashval = be32_to_cpu(sents[savehdr.count - 1].hashval); |
| 1604 | |
| 1605 | /* log the changes made when moving the entries */ |
Dave Chinner | 01ba43b | 2013-10-29 22:11:52 +1100 | [diff] [blame] | 1606 | dp->d_ops->leaf_hdr_to_disk(save_leaf, &savehdr); |
| 1607 | dp->d_ops->leaf_hdr_to_disk(drop_leaf, &drophdr); |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame] | 1608 | xfs_dir3_leaf_log_header(args, save_blk->bp); |
| 1609 | xfs_dir3_leaf_log_header(args, drop_blk->bp); |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1610 | |
Dave Chinner | 4141956 | 2013-10-29 22:11:50 +1100 | [diff] [blame] | 1611 | xfs_dir3_leaf_check(dp, save_blk->bp); |
| 1612 | xfs_dir3_leaf_check(dp, drop_blk->bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1613 | } |
| 1614 | |
| 1615 | /* |
| 1616 | * Top-level node form directory addname routine. |
| 1617 | */ |
| 1618 | int /* error */ |
| 1619 | xfs_dir2_node_addname( |
| 1620 | xfs_da_args_t *args) /* operation arguments */ |
| 1621 | { |
| 1622 | xfs_da_state_blk_t *blk; /* leaf block for insert */ |
| 1623 | int error; /* error return value */ |
| 1624 | int rval; /* sub-return value */ |
| 1625 | xfs_da_state_t *state; /* btree cursor */ |
| 1626 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1627 | trace_xfs_dir2_node_addname(args); |
| 1628 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1629 | /* |
| 1630 | * Allocate and initialize the state (btree cursor). |
| 1631 | */ |
| 1632 | state = xfs_da_state_alloc(); |
| 1633 | state->args = args; |
| 1634 | state->mp = args->dp->i_mount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1635 | /* |
| 1636 | * Look up the name. We're not supposed to find it, but |
| 1637 | * this gives us the insertion point. |
| 1638 | */ |
Dave Chinner | f5ea110 | 2013-04-24 18:58:02 +1000 | [diff] [blame] | 1639 | error = xfs_da3_node_lookup_int(state, &rval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1640 | if (error) |
| 1641 | rval = error; |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1642 | if (rval != -ENOENT) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1643 | goto done; |
| 1644 | } |
| 1645 | /* |
| 1646 | * Add the data entry to a data block. |
| 1647 | * Extravalid is set to a freeblock found by lookup. |
| 1648 | */ |
| 1649 | rval = xfs_dir2_node_addname_int(args, |
| 1650 | state->extravalid ? &state->extrablk : NULL); |
| 1651 | if (rval) { |
| 1652 | goto done; |
| 1653 | } |
| 1654 | blk = &state->path.blk[state->path.active - 1]; |
| 1655 | ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC); |
| 1656 | /* |
| 1657 | * Add the new leaf entry. |
| 1658 | */ |
| 1659 | rval = xfs_dir2_leafn_add(blk->bp, args, blk->index); |
| 1660 | if (rval == 0) { |
| 1661 | /* |
| 1662 | * It worked, fix the hash values up the btree. |
| 1663 | */ |
Barry Naujok | 6a17810 | 2008-05-21 16:42:05 +1000 | [diff] [blame] | 1664 | if (!(args->op_flags & XFS_DA_OP_JUSTCHECK)) |
Dave Chinner | f5ea110 | 2013-04-24 18:58:02 +1000 | [diff] [blame] | 1665 | xfs_da3_fixhashpath(state, &state->path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1666 | } else { |
| 1667 | /* |
| 1668 | * It didn't work, we need to split the leaf block. |
| 1669 | */ |
| 1670 | if (args->total == 0) { |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1671 | ASSERT(rval == -ENOSPC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1672 | goto done; |
| 1673 | } |
| 1674 | /* |
| 1675 | * Split the leaf block and insert the new entry. |
| 1676 | */ |
Dave Chinner | f5ea110 | 2013-04-24 18:58:02 +1000 | [diff] [blame] | 1677 | rval = xfs_da3_split(state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1678 | } |
| 1679 | done: |
| 1680 | xfs_da_state_free(state); |
| 1681 | return rval; |
| 1682 | } |
| 1683 | |
| 1684 | /* |
| 1685 | * Add the data entry for a node-format directory name addition. |
| 1686 | * The leaf entry is added in xfs_dir2_leafn_add. |
| 1687 | * We may enter with a freespace block that the lookup found. |
| 1688 | */ |
| 1689 | static int /* error */ |
| 1690 | xfs_dir2_node_addname_int( |
| 1691 | xfs_da_args_t *args, /* operation arguments */ |
| 1692 | xfs_da_state_blk_t *fblk) /* optional freespace block */ |
| 1693 | { |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 1694 | xfs_dir2_data_hdr_t *hdr; /* data block header */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1695 | xfs_dir2_db_t dbno; /* data block number */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1696 | struct xfs_buf *dbp; /* data block buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1697 | xfs_dir2_data_entry_t *dep; /* data entry pointer */ |
| 1698 | xfs_inode_t *dp; /* incore directory inode */ |
| 1699 | xfs_dir2_data_unused_t *dup; /* data unused entry pointer */ |
| 1700 | int error; /* error return value */ |
| 1701 | xfs_dir2_db_t fbno; /* freespace block number */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1702 | struct xfs_buf *fbp; /* freespace buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1703 | int findex; /* freespace entry index */ |
| 1704 | xfs_dir2_free_t *free=NULL; /* freespace block structure */ |
| 1705 | xfs_dir2_db_t ifbno; /* initial freespace block no */ |
| 1706 | xfs_dir2_db_t lastfbno=0; /* highest freespace block no */ |
| 1707 | int length; /* length of the new entry */ |
| 1708 | int logfree; /* need to log free entry */ |
| 1709 | xfs_mount_t *mp; /* filesystem mount point */ |
| 1710 | int needlog; /* need to log data header */ |
| 1711 | int needscan; /* need to rescan data frees */ |
Nathan Scott | 3d693c6 | 2006-03-17 17:28:27 +1100 | [diff] [blame] | 1712 | __be16 *tagp; /* data entry tag pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1713 | xfs_trans_t *tp; /* transaction pointer */ |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 1714 | __be16 *bests; |
| 1715 | struct xfs_dir3_icfree_hdr freehdr; |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame] | 1716 | struct xfs_dir2_data_free *bf; |
Darrick J. Wong | 6915ef3 | 2018-03-23 10:06:51 -0700 | [diff] [blame] | 1717 | xfs_dir2_data_aoff_t aoff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1718 | |
| 1719 | dp = args->dp; |
| 1720 | mp = dp->i_mount; |
| 1721 | tp = args->trans; |
Dave Chinner | 9d23fc8 | 2013-10-29 22:11:48 +1100 | [diff] [blame] | 1722 | length = dp->d_ops->data_entsize(args->namelen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1723 | /* |
| 1724 | * If we came in with a freespace block that means that lookup |
| 1725 | * found an entry with our hash value. This is the freespace |
| 1726 | * block for that data entry. |
| 1727 | */ |
| 1728 | if (fblk) { |
| 1729 | fbp = fblk->bp; |
| 1730 | /* |
| 1731 | * Remember initial freespace block number. |
| 1732 | */ |
| 1733 | ifbno = fblk->blkno; |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1734 | free = fbp->b_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1735 | findex = fblk->index; |
Dave Chinner | 24dd0f5 | 2013-10-30 13:48:41 -0500 | [diff] [blame] | 1736 | bests = dp->d_ops->free_bests_p(free); |
Dave Chinner | 01ba43b | 2013-10-29 22:11:52 +1100 | [diff] [blame] | 1737 | dp->d_ops->free_hdr_from_disk(&freehdr, free); |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 1738 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1739 | /* |
| 1740 | * This means the free entry showed that the data block had |
| 1741 | * space for our entry, so we remembered it. |
| 1742 | * Use that data block. |
| 1743 | */ |
| 1744 | if (findex >= 0) { |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 1745 | ASSERT(findex < freehdr.nvalid); |
| 1746 | ASSERT(be16_to_cpu(bests[findex]) != NULLDATAOFF); |
| 1747 | ASSERT(be16_to_cpu(bests[findex]) >= length); |
| 1748 | dbno = freehdr.firstdb + findex; |
| 1749 | } else { |
| 1750 | /* |
| 1751 | * The data block looked at didn't have enough room. |
| 1752 | * We'll start at the beginning of the freespace entries. |
| 1753 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1754 | dbno = -1; |
| 1755 | findex = 0; |
| 1756 | } |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 1757 | } else { |
| 1758 | /* |
| 1759 | * Didn't come in with a freespace block, so no data block. |
| 1760 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1761 | ifbno = dbno = -1; |
| 1762 | fbp = NULL; |
| 1763 | findex = 0; |
| 1764 | } |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 1765 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1766 | /* |
| 1767 | * If we don't have a data block yet, we're going to scan the |
| 1768 | * freespace blocks looking for one. Figure out what the |
| 1769 | * highest freespace block number is. |
| 1770 | */ |
| 1771 | if (dbno == -1) { |
| 1772 | xfs_fileoff_t fo; /* freespace block number */ |
| 1773 | |
Eric Sandeen | 7fb2cd4 | 2014-04-14 18:58:05 +1000 | [diff] [blame] | 1774 | if ((error = xfs_bmap_last_offset(dp, &fo, XFS_DATA_FORK))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1775 | return error; |
Dave Chinner | 2998ab1d | 2014-06-06 15:07:53 +1000 | [diff] [blame] | 1776 | lastfbno = xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)fo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1777 | fbno = ifbno; |
| 1778 | } |
| 1779 | /* |
| 1780 | * While we haven't identified a data block, search the freeblock |
| 1781 | * data for a good data block. If we find a null freeblock entry, |
| 1782 | * indicating a hole in the data blocks, remember that. |
| 1783 | */ |
| 1784 | while (dbno == -1) { |
| 1785 | /* |
| 1786 | * If we don't have a freeblock in hand, get the next one. |
| 1787 | */ |
| 1788 | if (fbp == NULL) { |
| 1789 | /* |
| 1790 | * Happens the first time through unless lookup gave |
| 1791 | * us a freespace block to start with. |
| 1792 | */ |
| 1793 | if (++fbno == 0) |
Dave Chinner | 3002803 | 2014-06-06 15:08:18 +1000 | [diff] [blame] | 1794 | fbno = xfs_dir2_byte_to_db(args->geo, |
Dave Chinner | 8c44a28 | 2014-06-06 15:04:41 +1000 | [diff] [blame] | 1795 | XFS_DIR2_FREE_OFFSET); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1796 | /* |
| 1797 | * If it's ifbno we already looked at it. |
| 1798 | */ |
| 1799 | if (fbno == ifbno) |
| 1800 | fbno++; |
| 1801 | /* |
| 1802 | * If it's off the end we're done. |
| 1803 | */ |
| 1804 | if (fbno >= lastfbno) |
| 1805 | break; |
| 1806 | /* |
| 1807 | * Read the block. There can be holes in the |
| 1808 | * freespace blocks, so this might not succeed. |
| 1809 | * This should be really rare, so there's no reason |
| 1810 | * to avoid it. |
| 1811 | */ |
Dave Chinner | 2025207 | 2012-11-12 22:54:13 +1100 | [diff] [blame] | 1812 | error = xfs_dir2_free_try_read(tp, dp, |
Dave Chinner | 2998ab1d | 2014-06-06 15:07:53 +1000 | [diff] [blame] | 1813 | xfs_dir2_db_to_da(args->geo, fbno), |
| 1814 | &fbp); |
Dave Chinner | 4bb20a8 | 2012-11-12 22:54:10 +1100 | [diff] [blame] | 1815 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1816 | return error; |
Dave Chinner | 4bb20a8 | 2012-11-12 22:54:10 +1100 | [diff] [blame] | 1817 | if (!fbp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1818 | continue; |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1819 | free = fbp->b_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1820 | findex = 0; |
| 1821 | } |
| 1822 | /* |
| 1823 | * Look at the current free entry. Is it good enough? |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 1824 | * |
| 1825 | * The bests initialisation should be where the bufer is read in |
| 1826 | * the above branch. But gcc is too stupid to realise that bests |
| 1827 | * and the freehdr are actually initialised if they are placed |
| 1828 | * there, so we have to do it here to avoid warnings. Blech. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1829 | */ |
Dave Chinner | 24dd0f5 | 2013-10-30 13:48:41 -0500 | [diff] [blame] | 1830 | bests = dp->d_ops->free_bests_p(free); |
Dave Chinner | 01ba43b | 2013-10-29 22:11:52 +1100 | [diff] [blame] | 1831 | dp->d_ops->free_hdr_from_disk(&freehdr, free); |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 1832 | if (be16_to_cpu(bests[findex]) != NULLDATAOFF && |
| 1833 | be16_to_cpu(bests[findex]) >= length) |
| 1834 | dbno = freehdr.firstdb + findex; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1835 | else { |
| 1836 | /* |
| 1837 | * Are we done with the freeblock? |
| 1838 | */ |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 1839 | if (++findex == freehdr.nvalid) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1840 | /* |
| 1841 | * Drop the block. |
| 1842 | */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1843 | xfs_trans_brelse(tp, fbp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1844 | fbp = NULL; |
| 1845 | if (fblk && fblk->bp) |
| 1846 | fblk->bp = NULL; |
| 1847 | } |
| 1848 | } |
| 1849 | } |
| 1850 | /* |
| 1851 | * If we don't have a data block, we need to allocate one and make |
| 1852 | * the freespace entries refer to it. |
| 1853 | */ |
| 1854 | if (unlikely(dbno == -1)) { |
| 1855 | /* |
| 1856 | * Not allowed to allocate, return failure. |
| 1857 | */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1858 | if ((args->op_flags & XFS_DA_OP_JUSTCHECK) || args->total == 0) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1859 | return -ENOSPC; |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1860 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1861 | /* |
| 1862 | * Allocate and initialize the new data block. |
| 1863 | */ |
| 1864 | if (unlikely((error = xfs_dir2_grow_inode(args, |
| 1865 | XFS_DIR2_DATA_SPACE, |
| 1866 | &dbno)) || |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 1867 | (error = xfs_dir3_data_init(args, dbno, &dbp)))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1868 | return error; |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1869 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1870 | /* |
| 1871 | * If (somehow) we have a freespace block, get rid of it. |
| 1872 | */ |
| 1873 | if (fbp) |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1874 | xfs_trans_brelse(tp, fbp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1875 | if (fblk && fblk->bp) |
| 1876 | fblk->bp = NULL; |
| 1877 | |
| 1878 | /* |
| 1879 | * Get the freespace block corresponding to the data block |
| 1880 | * that was just allocated. |
| 1881 | */ |
Dave Chinner | 8f66193 | 2014-06-06 15:15:59 +1000 | [diff] [blame] | 1882 | fbno = dp->d_ops->db_to_fdb(args->geo, dbno); |
Dave Chinner | 2025207 | 2012-11-12 22:54:13 +1100 | [diff] [blame] | 1883 | error = xfs_dir2_free_try_read(tp, dp, |
Dave Chinner | 2998ab1d | 2014-06-06 15:07:53 +1000 | [diff] [blame] | 1884 | xfs_dir2_db_to_da(args->geo, fbno), |
| 1885 | &fbp); |
Dave Chinner | 4bb20a8 | 2012-11-12 22:54:10 +1100 | [diff] [blame] | 1886 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1887 | return error; |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1888 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1889 | /* |
| 1890 | * If there wasn't a freespace block, the read will |
| 1891 | * return a NULL fbp. Allocate and initialize a new one. |
| 1892 | */ |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 1893 | if (!fbp) { |
| 1894 | error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, |
| 1895 | &fbno); |
| 1896 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1897 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1898 | |
Dave Chinner | 8f66193 | 2014-06-06 15:15:59 +1000 | [diff] [blame] | 1899 | if (dp->d_ops->db_to_fdb(args->geo, dbno) != fbno) { |
Dave Chinner | 0b932cc | 2011-03-07 10:08:35 +1100 | [diff] [blame] | 1900 | xfs_alert(mp, |
Joe Perches | f41febd | 2015-07-29 11:52:04 +1000 | [diff] [blame] | 1901 | "%s: dir ino %llu needed freesp block %lld for data block %lld, got %lld ifbno %llu lastfbno %d", |
Dave Chinner | 0b932cc | 2011-03-07 10:08:35 +1100 | [diff] [blame] | 1902 | __func__, (unsigned long long)dp->i_ino, |
Dave Chinner | 8f66193 | 2014-06-06 15:15:59 +1000 | [diff] [blame] | 1903 | (long long)dp->d_ops->db_to_fdb( |
| 1904 | args->geo, dbno), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1905 | (long long)dbno, (long long)fbno, |
| 1906 | (unsigned long long)ifbno, lastfbno); |
| 1907 | if (fblk) { |
Dave Chinner | 0b932cc | 2011-03-07 10:08:35 +1100 | [diff] [blame] | 1908 | xfs_alert(mp, |
Darrick J. Wong | c969004 | 2018-01-09 12:02:55 -0800 | [diff] [blame] | 1909 | " fblk "PTR_FMT" blkno %llu index %d magic 0x%x", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1910 | fblk, |
| 1911 | (unsigned long long)fblk->blkno, |
| 1912 | fblk->index, |
| 1913 | fblk->magic); |
| 1914 | } else { |
Dave Chinner | 0b932cc | 2011-03-07 10:08:35 +1100 | [diff] [blame] | 1915 | xfs_alert(mp, " ... fblk is NULL"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1916 | } |
| 1917 | XFS_ERROR_REPORT("xfs_dir2_node_addname_int", |
| 1918 | XFS_ERRLEVEL_LOW, mp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1919 | return -EFSCORRUPTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1920 | } |
| 1921 | |
| 1922 | /* |
| 1923 | * Get a buffer for the new block. |
| 1924 | */ |
Dave Chinner | 2998ab1d | 2014-06-06 15:07:53 +1000 | [diff] [blame] | 1925 | error = xfs_dir3_free_get_buf(args, fbno, &fbp); |
Dave Chinner | b0f539d | 2012-11-14 17:53:49 +1100 | [diff] [blame] | 1926 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1927 | return error; |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 1928 | free = fbp->b_addr; |
Dave Chinner | 24dd0f5 | 2013-10-30 13:48:41 -0500 | [diff] [blame] | 1929 | bests = dp->d_ops->free_bests_p(free); |
Dave Chinner | 01ba43b | 2013-10-29 22:11:52 +1100 | [diff] [blame] | 1930 | dp->d_ops->free_hdr_from_disk(&freehdr, free); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1931 | |
| 1932 | /* |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 1933 | * Remember the first slot as our empty slot. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1934 | */ |
Dave Chinner | 8c44a28 | 2014-06-06 15:04:41 +1000 | [diff] [blame] | 1935 | freehdr.firstdb = |
Dave Chinner | 3002803 | 2014-06-06 15:08:18 +1000 | [diff] [blame] | 1936 | (fbno - xfs_dir2_byte_to_db(args->geo, |
Dave Chinner | 8c44a28 | 2014-06-06 15:04:41 +1000 | [diff] [blame] | 1937 | XFS_DIR2_FREE_OFFSET)) * |
Dave Chinner | 8f66193 | 2014-06-06 15:15:59 +1000 | [diff] [blame] | 1938 | dp->d_ops->free_max_bests(args->geo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1939 | } else { |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1940 | free = fbp->b_addr; |
Dave Chinner | 24dd0f5 | 2013-10-30 13:48:41 -0500 | [diff] [blame] | 1941 | bests = dp->d_ops->free_bests_p(free); |
Dave Chinner | 01ba43b | 2013-10-29 22:11:52 +1100 | [diff] [blame] | 1942 | dp->d_ops->free_hdr_from_disk(&freehdr, free); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1943 | } |
| 1944 | |
| 1945 | /* |
| 1946 | * Set the freespace block index from the data block number. |
| 1947 | */ |
Dave Chinner | 8f66193 | 2014-06-06 15:15:59 +1000 | [diff] [blame] | 1948 | findex = dp->d_ops->db_to_fdindex(args->geo, dbno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1949 | /* |
| 1950 | * If it's after the end of the current entries in the |
| 1951 | * freespace block, extend that table. |
| 1952 | */ |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 1953 | if (findex >= freehdr.nvalid) { |
Dave Chinner | 8f66193 | 2014-06-06 15:15:59 +1000 | [diff] [blame] | 1954 | ASSERT(findex < dp->d_ops->free_max_bests(args->geo)); |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 1955 | freehdr.nvalid = findex + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1956 | /* |
| 1957 | * Tag new entry so nused will go up. |
| 1958 | */ |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 1959 | bests[findex] = cpu_to_be16(NULLDATAOFF); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1960 | } |
| 1961 | /* |
| 1962 | * If this entry was for an empty data block |
| 1963 | * (this should always be true) then update the header. |
| 1964 | */ |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 1965 | if (bests[findex] == cpu_to_be16(NULLDATAOFF)) { |
| 1966 | freehdr.nused++; |
Dave Chinner | 01ba43b | 2013-10-29 22:11:52 +1100 | [diff] [blame] | 1967 | dp->d_ops->free_hdr_to_disk(fbp->b_addr, &freehdr); |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame] | 1968 | xfs_dir2_free_log_header(args, fbp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1969 | } |
| 1970 | /* |
| 1971 | * Update the real value in the table. |
| 1972 | * We haven't allocated the data entry yet so this will |
| 1973 | * change again. |
| 1974 | */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1975 | hdr = dbp->b_addr; |
Dave Chinner | 2ca9877 | 2013-10-29 22:11:49 +1100 | [diff] [blame] | 1976 | bf = dp->d_ops->data_bestfree_p(hdr); |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame] | 1977 | bests[findex] = bf[0].length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1978 | logfree = 1; |
| 1979 | } |
| 1980 | /* |
| 1981 | * We had a data block so we don't have to make a new one. |
| 1982 | */ |
| 1983 | else { |
| 1984 | /* |
| 1985 | * If just checking, we succeeded. |
| 1986 | */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1987 | if (args->op_flags & XFS_DA_OP_JUSTCHECK) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1988 | return 0; |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1989 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1990 | /* |
| 1991 | * Read the data block in. |
| 1992 | */ |
Dave Chinner | 2998ab1d | 2014-06-06 15:07:53 +1000 | [diff] [blame] | 1993 | error = xfs_dir3_data_read(tp, dp, |
| 1994 | xfs_dir2_db_to_da(args->geo, dbno), |
Dave Chinner | e481357 | 2012-11-12 22:54:14 +1100 | [diff] [blame] | 1995 | -1, &dbp); |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1996 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1997 | return error; |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1998 | hdr = dbp->b_addr; |
Dave Chinner | 2ca9877 | 2013-10-29 22:11:49 +1100 | [diff] [blame] | 1999 | bf = dp->d_ops->data_bestfree_p(hdr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2000 | logfree = 0; |
| 2001 | } |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame] | 2002 | ASSERT(be16_to_cpu(bf[0].length) >= length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2003 | /* |
| 2004 | * Point to the existing unused space. |
| 2005 | */ |
| 2006 | dup = (xfs_dir2_data_unused_t *) |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame] | 2007 | ((char *)hdr + be16_to_cpu(bf[0].offset)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2008 | needscan = needlog = 0; |
| 2009 | /* |
| 2010 | * Mark the first part of the unused space, inuse for us. |
| 2011 | */ |
Darrick J. Wong | 6915ef3 | 2018-03-23 10:06:51 -0700 | [diff] [blame] | 2012 | aoff = (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr); |
| 2013 | error = xfs_dir2_data_use_free(args, dbp, dup, aoff, length, |
| 2014 | &needlog, &needscan); |
| 2015 | if (error) { |
| 2016 | xfs_trans_brelse(tp, dbp); |
| 2017 | return error; |
| 2018 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2019 | /* |
| 2020 | * Fill in the new entry and log it. |
| 2021 | */ |
| 2022 | dep = (xfs_dir2_data_entry_t *)dup; |
Christoph Hellwig | ff9901c | 2006-06-09 14:48:37 +1000 | [diff] [blame] | 2023 | dep->inumber = cpu_to_be64(args->inumber); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2024 | dep->namelen = args->namelen; |
| 2025 | memcpy(dep->name, args->name, dep->namelen); |
Dave Chinner | 9d23fc8 | 2013-10-29 22:11:48 +1100 | [diff] [blame] | 2026 | dp->d_ops->data_put_ftype(dep, args->filetype); |
| 2027 | tagp = dp->d_ops->data_entry_tag_p(dep); |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 2028 | *tagp = cpu_to_be16((char *)dep - (char *)hdr); |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame] | 2029 | xfs_dir2_data_log_entry(args, dbp, dep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2030 | /* |
| 2031 | * Rescan the block for bestfree if needed. |
| 2032 | */ |
| 2033 | if (needscan) |
Dave Chinner | 9d23fc8 | 2013-10-29 22:11:48 +1100 | [diff] [blame] | 2034 | xfs_dir2_data_freescan(dp, hdr, &needlog); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2035 | /* |
| 2036 | * Log the data block header if needed. |
| 2037 | */ |
| 2038 | if (needlog) |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame] | 2039 | xfs_dir2_data_log_header(args, dbp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2040 | /* |
| 2041 | * If the freespace entry is now wrong, update it. |
| 2042 | */ |
Dave Chinner | 24dd0f5 | 2013-10-30 13:48:41 -0500 | [diff] [blame] | 2043 | bests = dp->d_ops->free_bests_p(free); /* gcc is so stupid */ |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame] | 2044 | if (be16_to_cpu(bests[findex]) != be16_to_cpu(bf[0].length)) { |
| 2045 | bests[findex] = bf[0].length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2046 | logfree = 1; |
| 2047 | } |
| 2048 | /* |
| 2049 | * Log the freespace entry if needed. |
| 2050 | */ |
| 2051 | if (logfree) |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame] | 2052 | xfs_dir2_free_log_bests(args, fbp, findex, findex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2053 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2054 | * Return the data block and offset in args, then drop the data block. |
| 2055 | */ |
| 2056 | args->blkno = (xfs_dablk_t)dbno; |
Nathan Scott | 3d693c6 | 2006-03-17 17:28:27 +1100 | [diff] [blame] | 2057 | args->index = be16_to_cpu(*tagp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2058 | return 0; |
| 2059 | } |
| 2060 | |
| 2061 | /* |
| 2062 | * Lookup an entry in a node-format directory. |
Dave Chinner | f5ea110 | 2013-04-24 18:58:02 +1000 | [diff] [blame] | 2063 | * All the real work happens in xfs_da3_node_lookup_int. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2064 | * The only real output is the inode number of the entry. |
| 2065 | */ |
| 2066 | int /* error */ |
| 2067 | xfs_dir2_node_lookup( |
| 2068 | xfs_da_args_t *args) /* operation arguments */ |
| 2069 | { |
| 2070 | int error; /* error return value */ |
| 2071 | int i; /* btree level */ |
| 2072 | int rval; /* operation return value */ |
| 2073 | xfs_da_state_t *state; /* btree cursor */ |
| 2074 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2075 | trace_xfs_dir2_node_lookup(args); |
| 2076 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2077 | /* |
| 2078 | * Allocate and initialize the btree cursor. |
| 2079 | */ |
| 2080 | state = xfs_da_state_alloc(); |
| 2081 | state->args = args; |
| 2082 | state->mp = args->dp->i_mount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2083 | /* |
| 2084 | * Fill in the path to the entry in the cursor. |
| 2085 | */ |
Dave Chinner | f5ea110 | 2013-04-24 18:58:02 +1000 | [diff] [blame] | 2086 | error = xfs_da3_node_lookup_int(state, &rval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2087 | if (error) |
| 2088 | rval = error; |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 2089 | else if (rval == -ENOENT && args->cmpresult == XFS_CMP_CASE) { |
| 2090 | /* If a CI match, dup the actual name and return -EEXIST */ |
Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame] | 2091 | xfs_dir2_data_entry_t *dep; |
| 2092 | |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 2093 | dep = (xfs_dir2_data_entry_t *) |
| 2094 | ((char *)state->extrablk.bp->b_addr + |
| 2095 | state->extrablk.index); |
Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame] | 2096 | rval = xfs_dir_cilookup_result(args, dep->name, dep->namelen); |
| 2097 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2098 | /* |
| 2099 | * Release the btree blocks and leaf block. |
| 2100 | */ |
| 2101 | for (i = 0; i < state->path.active; i++) { |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 2102 | xfs_trans_brelse(args->trans, state->path.blk[i].bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2103 | state->path.blk[i].bp = NULL; |
| 2104 | } |
| 2105 | /* |
| 2106 | * Release the data block if we have it. |
| 2107 | */ |
| 2108 | if (state->extravalid && state->extrablk.bp) { |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 2109 | xfs_trans_brelse(args->trans, state->extrablk.bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2110 | state->extrablk.bp = NULL; |
| 2111 | } |
| 2112 | xfs_da_state_free(state); |
| 2113 | return rval; |
| 2114 | } |
| 2115 | |
| 2116 | /* |
| 2117 | * Remove an entry from a node-format directory. |
| 2118 | */ |
| 2119 | int /* error */ |
| 2120 | xfs_dir2_node_removename( |
Mark Tinguely | 3a8c920 | 2013-10-05 21:48:25 -0500 | [diff] [blame] | 2121 | struct xfs_da_args *args) /* operation arguments */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2122 | { |
Mark Tinguely | 3a8c920 | 2013-10-05 21:48:25 -0500 | [diff] [blame] | 2123 | struct xfs_da_state_blk *blk; /* leaf block */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2124 | int error; /* error return value */ |
| 2125 | int rval; /* operation return value */ |
Mark Tinguely | 3a8c920 | 2013-10-05 21:48:25 -0500 | [diff] [blame] | 2126 | struct xfs_da_state *state; /* btree cursor */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2127 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2128 | trace_xfs_dir2_node_removename(args); |
| 2129 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2130 | /* |
| 2131 | * Allocate and initialize the btree cursor. |
| 2132 | */ |
| 2133 | state = xfs_da_state_alloc(); |
| 2134 | state->args = args; |
| 2135 | state->mp = args->dp->i_mount; |
Mark Tinguely | 3a8c920 | 2013-10-05 21:48:25 -0500 | [diff] [blame] | 2136 | |
| 2137 | /* Look up the entry we're deleting, set up the cursor. */ |
Dave Chinner | f5ea110 | 2013-04-24 18:58:02 +1000 | [diff] [blame] | 2138 | error = xfs_da3_node_lookup_int(state, &rval); |
Barry Naujok | 5163f95 | 2008-05-21 16:41:01 +1000 | [diff] [blame] | 2139 | if (error) |
Mark Tinguely | 3a8c920 | 2013-10-05 21:48:25 -0500 | [diff] [blame] | 2140 | goto out_free; |
| 2141 | |
| 2142 | /* Didn't find it, upper layer screwed up. */ |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 2143 | if (rval != -EEXIST) { |
Mark Tinguely | 3a8c920 | 2013-10-05 21:48:25 -0500 | [diff] [blame] | 2144 | error = rval; |
| 2145 | goto out_free; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2146 | } |
Mark Tinguely | 3a8c920 | 2013-10-05 21:48:25 -0500 | [diff] [blame] | 2147 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2148 | blk = &state->path.blk[state->path.active - 1]; |
| 2149 | ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC); |
| 2150 | ASSERT(state->extravalid); |
| 2151 | /* |
| 2152 | * Remove the leaf and data entries. |
| 2153 | * Extrablk refers to the data block. |
| 2154 | */ |
| 2155 | error = xfs_dir2_leafn_remove(args, blk->bp, blk->index, |
| 2156 | &state->extrablk, &rval); |
Barry Naujok | 5163f95 | 2008-05-21 16:41:01 +1000 | [diff] [blame] | 2157 | if (error) |
Mark Tinguely | 3a8c920 | 2013-10-05 21:48:25 -0500 | [diff] [blame] | 2158 | goto out_free; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2159 | /* |
| 2160 | * Fix the hash values up the btree. |
| 2161 | */ |
Dave Chinner | f5ea110 | 2013-04-24 18:58:02 +1000 | [diff] [blame] | 2162 | xfs_da3_fixhashpath(state, &state->path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2163 | /* |
| 2164 | * If we need to join leaf blocks, do it. |
| 2165 | */ |
| 2166 | if (rval && state->path.active > 1) |
Dave Chinner | f5ea110 | 2013-04-24 18:58:02 +1000 | [diff] [blame] | 2167 | error = xfs_da3_join(state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2168 | /* |
| 2169 | * If no errors so far, try conversion to leaf format. |
| 2170 | */ |
| 2171 | if (!error) |
| 2172 | error = xfs_dir2_node_to_leaf(state); |
Mark Tinguely | 3a8c920 | 2013-10-05 21:48:25 -0500 | [diff] [blame] | 2173 | out_free: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2174 | xfs_da_state_free(state); |
| 2175 | return error; |
| 2176 | } |
| 2177 | |
| 2178 | /* |
| 2179 | * Replace an entry's inode number in a node-format directory. |
| 2180 | */ |
| 2181 | int /* error */ |
| 2182 | xfs_dir2_node_replace( |
| 2183 | xfs_da_args_t *args) /* operation arguments */ |
| 2184 | { |
| 2185 | xfs_da_state_blk_t *blk; /* leaf block */ |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 2186 | xfs_dir2_data_hdr_t *hdr; /* data block header */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2187 | xfs_dir2_data_entry_t *dep; /* data entry changed */ |
| 2188 | int error; /* error return value */ |
| 2189 | int i; /* btree level */ |
| 2190 | xfs_ino_t inum; /* new inode number */ |
Jan Kara | 0375423 | 2015-08-25 10:05:13 +1000 | [diff] [blame] | 2191 | int ftype; /* new file type */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2192 | xfs_dir2_leaf_t *leaf; /* leaf structure */ |
| 2193 | xfs_dir2_leaf_entry_t *lep; /* leaf entry being changed */ |
| 2194 | int rval; /* internal return value */ |
| 2195 | xfs_da_state_t *state; /* btree cursor */ |
| 2196 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2197 | trace_xfs_dir2_node_replace(args); |
| 2198 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2199 | /* |
| 2200 | * Allocate and initialize the btree cursor. |
| 2201 | */ |
| 2202 | state = xfs_da_state_alloc(); |
| 2203 | state->args = args; |
| 2204 | state->mp = args->dp->i_mount; |
Jan Kara | 0375423 | 2015-08-25 10:05:13 +1000 | [diff] [blame] | 2205 | |
| 2206 | /* |
| 2207 | * We have to save new inode number and ftype since |
| 2208 | * xfs_da3_node_lookup_int() is going to overwrite them |
| 2209 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2210 | inum = args->inumber; |
Jan Kara | 0375423 | 2015-08-25 10:05:13 +1000 | [diff] [blame] | 2211 | ftype = args->filetype; |
| 2212 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2213 | /* |
| 2214 | * Lookup the entry to change in the btree. |
| 2215 | */ |
Dave Chinner | f5ea110 | 2013-04-24 18:58:02 +1000 | [diff] [blame] | 2216 | error = xfs_da3_node_lookup_int(state, &rval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2217 | if (error) { |
| 2218 | rval = error; |
| 2219 | } |
| 2220 | /* |
| 2221 | * It should be found, since the vnodeops layer has looked it up |
| 2222 | * and locked it. But paranoia is good. |
| 2223 | */ |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 2224 | if (rval == -EEXIST) { |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 2225 | struct xfs_dir2_leaf_entry *ents; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2226 | /* |
| 2227 | * Find the leaf entry. |
| 2228 | */ |
| 2229 | blk = &state->path.blk[state->path.active - 1]; |
| 2230 | ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC); |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 2231 | leaf = blk->bp->b_addr; |
Dave Chinner | 4141956 | 2013-10-29 22:11:50 +1100 | [diff] [blame] | 2232 | ents = args->dp->d_ops->leaf_ents_p(leaf); |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 2233 | lep = &ents[blk->index]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2234 | ASSERT(state->extravalid); |
| 2235 | /* |
| 2236 | * Point to the data entry. |
| 2237 | */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 2238 | hdr = state->extrablk.bp->b_addr; |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame] | 2239 | ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) || |
| 2240 | hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2241 | dep = (xfs_dir2_data_entry_t *) |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 2242 | ((char *)hdr + |
Dave Chinner | 3002803 | 2014-06-06 15:08:18 +1000 | [diff] [blame] | 2243 | xfs_dir2_dataptr_to_off(args->geo, |
| 2244 | be32_to_cpu(lep->address))); |
Christoph Hellwig | ff9901c | 2006-06-09 14:48:37 +1000 | [diff] [blame] | 2245 | ASSERT(inum != be64_to_cpu(dep->inumber)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2246 | /* |
| 2247 | * Fill in the new inode number and log the entry. |
| 2248 | */ |
Christoph Hellwig | ff9901c | 2006-06-09 14:48:37 +1000 | [diff] [blame] | 2249 | dep->inumber = cpu_to_be64(inum); |
Jan Kara | 0375423 | 2015-08-25 10:05:13 +1000 | [diff] [blame] | 2250 | args->dp->d_ops->data_put_ftype(dep, ftype); |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame] | 2251 | xfs_dir2_data_log_entry(args, state->extrablk.bp, dep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2252 | rval = 0; |
| 2253 | } |
| 2254 | /* |
| 2255 | * Didn't find it, and we're holding a data block. Drop it. |
| 2256 | */ |
| 2257 | else if (state->extravalid) { |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 2258 | xfs_trans_brelse(args->trans, state->extrablk.bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2259 | state->extrablk.bp = NULL; |
| 2260 | } |
| 2261 | /* |
| 2262 | * Release all the buffers in the cursor. |
| 2263 | */ |
| 2264 | for (i = 0; i < state->path.active; i++) { |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 2265 | xfs_trans_brelse(args->trans, state->path.blk[i].bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2266 | state->path.blk[i].bp = NULL; |
| 2267 | } |
| 2268 | xfs_da_state_free(state); |
| 2269 | return rval; |
| 2270 | } |
| 2271 | |
| 2272 | /* |
| 2273 | * Trim off a trailing empty freespace block. |
| 2274 | * Return (in rvalp) 1 if we did it, 0 if not. |
| 2275 | */ |
| 2276 | int /* error */ |
| 2277 | xfs_dir2_node_trim_free( |
| 2278 | xfs_da_args_t *args, /* operation arguments */ |
| 2279 | xfs_fileoff_t fo, /* free block number */ |
| 2280 | int *rvalp) /* out: did something */ |
| 2281 | { |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 2282 | struct xfs_buf *bp; /* freespace buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2283 | xfs_inode_t *dp; /* incore directory inode */ |
| 2284 | int error; /* error return code */ |
| 2285 | xfs_dir2_free_t *free; /* freespace structure */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2286 | xfs_trans_t *tp; /* transaction pointer */ |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 2287 | struct xfs_dir3_icfree_hdr freehdr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2288 | |
| 2289 | dp = args->dp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2290 | tp = args->trans; |
Christoph Hellwig | 355cced | 2016-03-15 11:44:18 +1100 | [diff] [blame] | 2291 | |
| 2292 | *rvalp = 0; |
| 2293 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2294 | /* |
| 2295 | * Read the freespace block. |
| 2296 | */ |
Dave Chinner | 2025207 | 2012-11-12 22:54:13 +1100 | [diff] [blame] | 2297 | error = xfs_dir2_free_try_read(tp, dp, fo, &bp); |
Dave Chinner | 4bb20a8 | 2012-11-12 22:54:10 +1100 | [diff] [blame] | 2298 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2299 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2300 | /* |
| 2301 | * There can be holes in freespace. If fo is a hole, there's |
| 2302 | * nothing to do. |
| 2303 | */ |
Dave Chinner | 2025207 | 2012-11-12 22:54:13 +1100 | [diff] [blame] | 2304 | if (!bp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2305 | return 0; |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 2306 | free = bp->b_addr; |
Dave Chinner | 01ba43b | 2013-10-29 22:11:52 +1100 | [diff] [blame] | 2307 | dp->d_ops->free_hdr_from_disk(&freehdr, free); |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 2308 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2309 | /* |
| 2310 | * If there are used entries, there's nothing to do. |
| 2311 | */ |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 2312 | if (freehdr.nused > 0) { |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 2313 | xfs_trans_brelse(tp, bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2314 | return 0; |
| 2315 | } |
| 2316 | /* |
| 2317 | * Blow the block away. |
| 2318 | */ |
Dave Chinner | 2998ab1d | 2014-06-06 15:07:53 +1000 | [diff] [blame] | 2319 | error = xfs_dir2_shrink_inode(args, |
| 2320 | xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)fo), bp); |
| 2321 | if (error) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2322 | /* |
| 2323 | * Can't fail with ENOSPC since that only happens with no |
| 2324 | * space reservation, when breaking up an extent into two |
| 2325 | * pieces. This is the last block of an extent. |
| 2326 | */ |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 2327 | ASSERT(error != -ENOSPC); |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 2328 | xfs_trans_brelse(tp, bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2329 | return error; |
| 2330 | } |
| 2331 | /* |
| 2332 | * Return that we succeeded. |
| 2333 | */ |
| 2334 | *rvalp = 1; |
| 2335 | return 0; |
| 2336 | } |