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