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