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