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