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