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