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