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