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