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-2003,2005 Silicon Graphics, Inc. |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 3 | * Copyright (c) 2013 Red Hat, Inc. |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 4 | * All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License as |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * published by the Free Software Foundation. |
| 9 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 10 | * This program is distributed in the hope that it would be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write the Free Software Foundation, |
| 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include "xfs.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 20 | #include "xfs_fs.h" |
Dave Chinner | a4fbe6a | 2013-10-23 10:51:50 +1100 | [diff] [blame] | 21 | #include "xfs_format.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 22 | #include "xfs_log_format.h" |
| 23 | #include "xfs_trans_resv.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include "xfs_sb.h" |
| 25 | #include "xfs_ag.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include "xfs_mount.h" |
Dave Chinner | 5706278 | 2013-10-15 09:17:51 +1100 | [diff] [blame] | 27 | #include "xfs_da_format.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 28 | #include "xfs_da_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include "xfs_inode.h" |
| 30 | #include "xfs_bmap.h" |
Dave Chinner | 2b9ab5a | 2013-08-12 20:49:37 +1000 | [diff] [blame] | 31 | #include "xfs_dir2.h" |
Christoph Hellwig | 5792664 | 2011-07-13 13:43:48 +0200 | [diff] [blame] | 32 | #include "xfs_dir2_priv.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include "xfs_error.h" |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 34 | #include "xfs_trace.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 35 | #include "xfs_trans.h" |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 36 | #include "xfs_buf_item.h" |
| 37 | #include "xfs_cksum.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
| 39 | /* |
| 40 | * Local function declarations. |
| 41 | */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 42 | static int xfs_dir2_leaf_lookup_int(xfs_da_args_t *args, struct xfs_buf **lbpp, |
| 43 | int *indexp, struct xfs_buf **dbpp); |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame^] | 44 | static void xfs_dir3_leaf_log_bests(struct xfs_da_args *args, |
| 45 | struct xfs_buf *bp, int first, int last); |
| 46 | static void xfs_dir3_leaf_log_tail(struct xfs_da_args *args, |
| 47 | struct xfs_buf *bp); |
Christoph Hellwig | ba0f32d | 2005-06-21 15:36:52 +1000 | [diff] [blame] | 48 | |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 49 | /* |
| 50 | * Check the internal consistency of a leaf1 block. |
| 51 | * Pop an assert if something is wrong. |
| 52 | */ |
| 53 | #ifdef DEBUG |
Dave Chinner | 4141956 | 2013-10-29 22:11:50 +1100 | [diff] [blame] | 54 | #define xfs_dir3_leaf_check(dp, bp) \ |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 55 | do { \ |
Dave Chinner | 4141956 | 2013-10-29 22:11:50 +1100 | [diff] [blame] | 56 | if (!xfs_dir3_leaf1_check((dp), (bp))) \ |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 57 | ASSERT(0); \ |
| 58 | } while (0); |
| 59 | |
| 60 | STATIC bool |
| 61 | xfs_dir3_leaf1_check( |
Dave Chinner | 4141956 | 2013-10-29 22:11:50 +1100 | [diff] [blame] | 62 | struct xfs_inode *dp, |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 63 | struct xfs_buf *bp) |
| 64 | { |
| 65 | struct xfs_dir2_leaf *leaf = bp->b_addr; |
| 66 | struct xfs_dir3_icleaf_hdr leafhdr; |
| 67 | |
Dave Chinner | 01ba43b | 2013-10-29 22:11:52 +1100 | [diff] [blame] | 68 | dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf); |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 69 | |
| 70 | if (leafhdr.magic == XFS_DIR3_LEAF1_MAGIC) { |
| 71 | struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr; |
| 72 | if (be64_to_cpu(leaf3->info.blkno) != bp->b_bn) |
| 73 | return false; |
| 74 | } else if (leafhdr.magic != XFS_DIR2_LEAF1_MAGIC) |
| 75 | return false; |
| 76 | |
Dave Chinner | 4141956 | 2013-10-29 22:11:50 +1100 | [diff] [blame] | 77 | return xfs_dir3_leaf_check_int(dp->i_mount, dp, &leafhdr, leaf); |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 78 | } |
| 79 | #else |
Dave Chinner | 4141956 | 2013-10-29 22:11:50 +1100 | [diff] [blame] | 80 | #define xfs_dir3_leaf_check(dp, bp) |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 81 | #endif |
| 82 | |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 83 | bool |
| 84 | xfs_dir3_leaf_check_int( |
| 85 | struct xfs_mount *mp, |
Dave Chinner | 4141956 | 2013-10-29 22:11:50 +1100 | [diff] [blame] | 86 | struct xfs_inode *dp, |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 87 | struct xfs_dir3_icleaf_hdr *hdr, |
| 88 | struct xfs_dir2_leaf *leaf) |
| 89 | { |
| 90 | struct xfs_dir2_leaf_entry *ents; |
| 91 | xfs_dir2_leaf_tail_t *ltp; |
| 92 | int stale; |
| 93 | int i; |
Dave Chinner | 4141956 | 2013-10-29 22:11:50 +1100 | [diff] [blame] | 94 | const struct xfs_dir_ops *ops; |
Dave Chinner | 01ba43b | 2013-10-29 22:11:52 +1100 | [diff] [blame] | 95 | struct xfs_dir3_icleaf_hdr leafhdr; |
Dave Chinner | 8f66193 | 2014-06-06 15:15:59 +1000 | [diff] [blame] | 96 | struct xfs_da_geometry *geo = mp->m_dir_geo; |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 97 | |
Dave Chinner | 4141956 | 2013-10-29 22:11:50 +1100 | [diff] [blame] | 98 | /* |
| 99 | * we can be passed a null dp here from a verifier, so we need to go the |
| 100 | * hard way to get them. |
| 101 | */ |
| 102 | ops = xfs_dir_get_ops(mp, dp); |
| 103 | |
Dave Chinner | 01ba43b | 2013-10-29 22:11:52 +1100 | [diff] [blame] | 104 | if (!hdr) { |
| 105 | ops->leaf_hdr_from_disk(&leafhdr, leaf); |
| 106 | hdr = &leafhdr; |
| 107 | } |
| 108 | |
Dave Chinner | 4141956 | 2013-10-29 22:11:50 +1100 | [diff] [blame] | 109 | ents = ops->leaf_ents_p(leaf); |
Dave Chinner | 8f66193 | 2014-06-06 15:15:59 +1000 | [diff] [blame] | 110 | ltp = xfs_dir2_leaf_tail_p(geo, leaf); |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 111 | |
| 112 | /* |
| 113 | * XXX (dgc): This value is not restrictive enough. |
| 114 | * Should factor in the size of the bests table as well. |
| 115 | * We can deduce a value for that from di_size. |
| 116 | */ |
Dave Chinner | 8f66193 | 2014-06-06 15:15:59 +1000 | [diff] [blame] | 117 | if (hdr->count > ops->leaf_max_ents(geo)) |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 118 | return false; |
| 119 | |
| 120 | /* Leaves and bests don't overlap in leaf format. */ |
| 121 | if ((hdr->magic == XFS_DIR2_LEAF1_MAGIC || |
| 122 | hdr->magic == XFS_DIR3_LEAF1_MAGIC) && |
| 123 | (char *)&ents[hdr->count] > (char *)xfs_dir2_leaf_bests_p(ltp)) |
| 124 | return false; |
| 125 | |
| 126 | /* Check hash value order, count stale entries. */ |
| 127 | for (i = stale = 0; i < hdr->count; i++) { |
| 128 | if (i + 1 < hdr->count) { |
| 129 | if (be32_to_cpu(ents[i].hashval) > |
| 130 | be32_to_cpu(ents[i + 1].hashval)) |
| 131 | return false; |
| 132 | } |
| 133 | if (ents[i].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) |
| 134 | stale++; |
| 135 | } |
| 136 | if (hdr->stale != stale) |
| 137 | return false; |
| 138 | return true; |
| 139 | } |
| 140 | |
Dave Chinner | 0f295a2 | 2013-09-03 10:06:58 +1000 | [diff] [blame] | 141 | /* |
| 142 | * We verify the magic numbers before decoding the leaf header so that on debug |
| 143 | * kernels we don't get assertion failures in xfs_dir3_leaf_hdr_from_disk() due |
| 144 | * to incorrect magic numbers. |
| 145 | */ |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 146 | static bool |
| 147 | xfs_dir3_leaf_verify( |
Dave Chinner | e6f7667 | 2012-11-12 22:54:15 +1100 | [diff] [blame] | 148 | struct xfs_buf *bp, |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 149 | __uint16_t magic) |
Dave Chinner | e6f7667 | 2012-11-12 22:54:15 +1100 | [diff] [blame] | 150 | { |
| 151 | struct xfs_mount *mp = bp->b_target->bt_mount; |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 152 | struct xfs_dir2_leaf *leaf = bp->b_addr; |
Dave Chinner | e6f7667 | 2012-11-12 22:54:15 +1100 | [diff] [blame] | 153 | |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 154 | ASSERT(magic == XFS_DIR2_LEAF1_MAGIC || magic == XFS_DIR2_LEAFN_MAGIC); |
| 155 | |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 156 | if (xfs_sb_version_hascrc(&mp->m_sb)) { |
| 157 | struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr; |
Dave Chinner | 0f295a2 | 2013-09-03 10:06:58 +1000 | [diff] [blame] | 158 | __uint16_t magic3; |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 159 | |
Dave Chinner | 0f295a2 | 2013-09-03 10:06:58 +1000 | [diff] [blame] | 160 | magic3 = (magic == XFS_DIR2_LEAF1_MAGIC) ? XFS_DIR3_LEAF1_MAGIC |
| 161 | : XFS_DIR3_LEAFN_MAGIC; |
| 162 | |
| 163 | if (leaf3->info.hdr.magic != cpu_to_be16(magic3)) |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 164 | return false; |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 165 | if (!uuid_equal(&leaf3->info.uuid, &mp->m_sb.sb_uuid)) |
| 166 | return false; |
| 167 | if (be64_to_cpu(leaf3->info.blkno) != bp->b_bn) |
| 168 | return false; |
| 169 | } else { |
Dave Chinner | 0f295a2 | 2013-09-03 10:06:58 +1000 | [diff] [blame] | 170 | if (leaf->hdr.info.magic != cpu_to_be16(magic)) |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 171 | return false; |
| 172 | } |
Dave Chinner | 0f295a2 | 2013-09-03 10:06:58 +1000 | [diff] [blame] | 173 | |
Dave Chinner | 01ba43b | 2013-10-29 22:11:52 +1100 | [diff] [blame] | 174 | return xfs_dir3_leaf_check_int(mp, NULL, NULL, leaf); |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | static void |
| 178 | __read_verify( |
| 179 | struct xfs_buf *bp, |
| 180 | __uint16_t magic) |
| 181 | { |
| 182 | struct xfs_mount *mp = bp->b_target->bt_mount; |
| 183 | |
Eric Sandeen | ce5028c | 2014-02-27 15:23:10 +1100 | [diff] [blame] | 184 | if (xfs_sb_version_hascrc(&mp->m_sb) && |
| 185 | !xfs_buf_verify_cksum(bp, XFS_DIR3_LEAF_CRC_OFF)) |
| 186 | xfs_buf_ioerror(bp, EFSBADCRC); |
| 187 | else if (!xfs_dir3_leaf_verify(bp, magic)) |
Dave Chinner | e6f7667 | 2012-11-12 22:54:15 +1100 | [diff] [blame] | 188 | xfs_buf_ioerror(bp, EFSCORRUPTED); |
Eric Sandeen | ce5028c | 2014-02-27 15:23:10 +1100 | [diff] [blame] | 189 | |
| 190 | if (bp->b_error) |
| 191 | xfs_verifier_error(bp); |
Dave Chinner | 612cfbf | 2012-11-14 17:52:32 +1100 | [diff] [blame] | 192 | } |
Dave Chinner | e6f7667 | 2012-11-12 22:54:15 +1100 | [diff] [blame] | 193 | |
Dave Chinner | 612cfbf | 2012-11-14 17:52:32 +1100 | [diff] [blame] | 194 | static void |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 195 | __write_verify( |
| 196 | struct xfs_buf *bp, |
| 197 | __uint16_t magic) |
Dave Chinner | 612cfbf | 2012-11-14 17:52:32 +1100 | [diff] [blame] | 198 | { |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 199 | struct xfs_mount *mp = bp->b_target->bt_mount; |
| 200 | struct xfs_buf_log_item *bip = bp->b_fspriv; |
| 201 | struct xfs_dir3_leaf_hdr *hdr3 = bp->b_addr; |
| 202 | |
| 203 | if (!xfs_dir3_leaf_verify(bp, magic)) { |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 204 | xfs_buf_ioerror(bp, EFSCORRUPTED); |
Eric Sandeen | ce5028c | 2014-02-27 15:23:10 +1100 | [diff] [blame] | 205 | xfs_verifier_error(bp); |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 206 | return; |
| 207 | } |
| 208 | |
| 209 | if (!xfs_sb_version_hascrc(&mp->m_sb)) |
| 210 | return; |
| 211 | |
| 212 | if (bip) |
| 213 | hdr3->info.lsn = cpu_to_be64(bip->bli_item.li_lsn); |
| 214 | |
Eric Sandeen | f1dbcd7 | 2014-02-27 15:18:23 +1100 | [diff] [blame] | 215 | xfs_buf_update_cksum(bp, XFS_DIR3_LEAF_CRC_OFF); |
Dave Chinner | 612cfbf | 2012-11-14 17:52:32 +1100 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | static void |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 219 | xfs_dir3_leaf1_read_verify( |
Dave Chinner | 612cfbf | 2012-11-14 17:52:32 +1100 | [diff] [blame] | 220 | struct xfs_buf *bp) |
| 221 | { |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 222 | __read_verify(bp, XFS_DIR2_LEAF1_MAGIC); |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 223 | } |
| 224 | |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 225 | static void |
| 226 | xfs_dir3_leaf1_write_verify( |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 227 | struct xfs_buf *bp) |
| 228 | { |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 229 | __write_verify(bp, XFS_DIR2_LEAF1_MAGIC); |
Dave Chinner | e6f7667 | 2012-11-12 22:54:15 +1100 | [diff] [blame] | 230 | } |
| 231 | |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 232 | static void |
| 233 | xfs_dir3_leafn_read_verify( |
Dave Chinner | 612cfbf | 2012-11-14 17:52:32 +1100 | [diff] [blame] | 234 | struct xfs_buf *bp) |
Dave Chinner | e6f7667 | 2012-11-12 22:54:15 +1100 | [diff] [blame] | 235 | { |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 236 | __read_verify(bp, XFS_DIR2_LEAFN_MAGIC); |
Dave Chinner | e6f7667 | 2012-11-12 22:54:15 +1100 | [diff] [blame] | 237 | } |
| 238 | |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 239 | static void |
| 240 | xfs_dir3_leafn_write_verify( |
| 241 | struct xfs_buf *bp) |
| 242 | { |
| 243 | __write_verify(bp, XFS_DIR2_LEAFN_MAGIC); |
| 244 | } |
| 245 | |
Dave Chinner | d75afeb | 2013-04-03 16:11:29 +1100 | [diff] [blame] | 246 | const struct xfs_buf_ops xfs_dir3_leaf1_buf_ops = { |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 247 | .verify_read = xfs_dir3_leaf1_read_verify, |
| 248 | .verify_write = xfs_dir3_leaf1_write_verify, |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 249 | }; |
| 250 | |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 251 | const struct xfs_buf_ops xfs_dir3_leafn_buf_ops = { |
| 252 | .verify_read = xfs_dir3_leafn_read_verify, |
| 253 | .verify_write = xfs_dir3_leafn_write_verify, |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 254 | }; |
Dave Chinner | e6f7667 | 2012-11-12 22:54:15 +1100 | [diff] [blame] | 255 | |
| 256 | static int |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 257 | xfs_dir3_leaf_read( |
Dave Chinner | e6f7667 | 2012-11-12 22:54:15 +1100 | [diff] [blame] | 258 | struct xfs_trans *tp, |
| 259 | struct xfs_inode *dp, |
| 260 | xfs_dablk_t fbno, |
| 261 | xfs_daddr_t mappedbno, |
| 262 | struct xfs_buf **bpp) |
| 263 | { |
Dave Chinner | d75afeb | 2013-04-03 16:11:29 +1100 | [diff] [blame] | 264 | int err; |
| 265 | |
| 266 | err = xfs_da_read_buf(tp, dp, fbno, mappedbno, bpp, |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 267 | XFS_DATA_FORK, &xfs_dir3_leaf1_buf_ops); |
Dave Chinner | d75afeb | 2013-04-03 16:11:29 +1100 | [diff] [blame] | 268 | if (!err && tp) |
Dave Chinner | 61fe135 | 2013-04-03 16:11:30 +1100 | [diff] [blame] | 269 | xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_LEAF1_BUF); |
Dave Chinner | d75afeb | 2013-04-03 16:11:29 +1100 | [diff] [blame] | 270 | return err; |
Dave Chinner | e6f7667 | 2012-11-12 22:54:15 +1100 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | int |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 274 | xfs_dir3_leafn_read( |
Dave Chinner | e6f7667 | 2012-11-12 22:54:15 +1100 | [diff] [blame] | 275 | struct xfs_trans *tp, |
| 276 | struct xfs_inode *dp, |
| 277 | xfs_dablk_t fbno, |
| 278 | xfs_daddr_t mappedbno, |
| 279 | struct xfs_buf **bpp) |
| 280 | { |
Dave Chinner | d75afeb | 2013-04-03 16:11:29 +1100 | [diff] [blame] | 281 | int err; |
| 282 | |
| 283 | err = xfs_da_read_buf(tp, dp, fbno, mappedbno, bpp, |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 284 | XFS_DATA_FORK, &xfs_dir3_leafn_buf_ops); |
Dave Chinner | d75afeb | 2013-04-03 16:11:29 +1100 | [diff] [blame] | 285 | if (!err && tp) |
Dave Chinner | 61fe135 | 2013-04-03 16:11:30 +1100 | [diff] [blame] | 286 | xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_LEAFN_BUF); |
Dave Chinner | d75afeb | 2013-04-03 16:11:29 +1100 | [diff] [blame] | 287 | return err; |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | /* |
| 291 | * Initialize a new leaf block, leaf1 or leafn magic accepted. |
| 292 | */ |
| 293 | static void |
| 294 | xfs_dir3_leaf_init( |
| 295 | struct xfs_mount *mp, |
Dave Chinner | d75afeb | 2013-04-03 16:11:29 +1100 | [diff] [blame] | 296 | struct xfs_trans *tp, |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 297 | struct xfs_buf *bp, |
| 298 | xfs_ino_t owner, |
| 299 | __uint16_t type) |
| 300 | { |
| 301 | struct xfs_dir2_leaf *leaf = bp->b_addr; |
| 302 | |
| 303 | ASSERT(type == XFS_DIR2_LEAF1_MAGIC || type == XFS_DIR2_LEAFN_MAGIC); |
| 304 | |
| 305 | if (xfs_sb_version_hascrc(&mp->m_sb)) { |
| 306 | struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr; |
| 307 | |
| 308 | memset(leaf3, 0, sizeof(*leaf3)); |
| 309 | |
| 310 | leaf3->info.hdr.magic = (type == XFS_DIR2_LEAF1_MAGIC) |
| 311 | ? cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) |
| 312 | : cpu_to_be16(XFS_DIR3_LEAFN_MAGIC); |
| 313 | leaf3->info.blkno = cpu_to_be64(bp->b_bn); |
| 314 | leaf3->info.owner = cpu_to_be64(owner); |
| 315 | uuid_copy(&leaf3->info.uuid, &mp->m_sb.sb_uuid); |
| 316 | } else { |
| 317 | memset(leaf, 0, sizeof(*leaf)); |
| 318 | leaf->hdr.info.magic = cpu_to_be16(type); |
| 319 | } |
| 320 | |
| 321 | /* |
| 322 | * If it's a leaf-format directory initialize the tail. |
| 323 | * Caller is responsible for initialising the bests table. |
| 324 | */ |
| 325 | if (type == XFS_DIR2_LEAF1_MAGIC) { |
| 326 | struct xfs_dir2_leaf_tail *ltp; |
| 327 | |
Dave Chinner | 8f66193 | 2014-06-06 15:15:59 +1000 | [diff] [blame] | 328 | ltp = xfs_dir2_leaf_tail_p(mp->m_dir_geo, leaf); |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 329 | ltp->bestcount = 0; |
| 330 | bp->b_ops = &xfs_dir3_leaf1_buf_ops; |
Dave Chinner | 61fe135 | 2013-04-03 16:11:30 +1100 | [diff] [blame] | 331 | xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_LEAF1_BUF); |
Dave Chinner | d75afeb | 2013-04-03 16:11:29 +1100 | [diff] [blame] | 332 | } else { |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 333 | bp->b_ops = &xfs_dir3_leafn_buf_ops; |
Dave Chinner | 61fe135 | 2013-04-03 16:11:30 +1100 | [diff] [blame] | 334 | xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_LEAFN_BUF); |
Dave Chinner | d75afeb | 2013-04-03 16:11:29 +1100 | [diff] [blame] | 335 | } |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | int |
| 339 | xfs_dir3_leaf_get_buf( |
| 340 | xfs_da_args_t *args, |
| 341 | xfs_dir2_db_t bno, |
| 342 | struct xfs_buf **bpp, |
| 343 | __uint16_t magic) |
| 344 | { |
| 345 | struct xfs_inode *dp = args->dp; |
| 346 | struct xfs_trans *tp = args->trans; |
| 347 | struct xfs_mount *mp = dp->i_mount; |
| 348 | struct xfs_buf *bp; |
| 349 | int error; |
| 350 | |
| 351 | ASSERT(magic == XFS_DIR2_LEAF1_MAGIC || magic == XFS_DIR2_LEAFN_MAGIC); |
Dave Chinner | 3002803 | 2014-06-06 15:08:18 +1000 | [diff] [blame] | 352 | ASSERT(bno >= xfs_dir2_byte_to_db(args->geo, XFS_DIR2_LEAF_OFFSET) && |
| 353 | bno < xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET)); |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 354 | |
Dave Chinner | 2998ab1 | 2014-06-06 15:07:53 +1000 | [diff] [blame] | 355 | error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(args->geo, bno), |
| 356 | -1, &bp, XFS_DATA_FORK); |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 357 | if (error) |
| 358 | return error; |
| 359 | |
Dave Chinner | d75afeb | 2013-04-03 16:11:29 +1100 | [diff] [blame] | 360 | xfs_dir3_leaf_init(mp, tp, bp, dp->i_ino, magic); |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame^] | 361 | xfs_dir3_leaf_log_header(args, bp); |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 362 | if (magic == XFS_DIR2_LEAF1_MAGIC) |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame^] | 363 | xfs_dir3_leaf_log_tail(args, bp); |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 364 | *bpp = bp; |
| 365 | return 0; |
Dave Chinner | e6f7667 | 2012-11-12 22:54:15 +1100 | [diff] [blame] | 366 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | |
| 368 | /* |
| 369 | * Convert a block form directory to a leaf form directory. |
| 370 | */ |
| 371 | int /* error */ |
| 372 | xfs_dir2_block_to_leaf( |
| 373 | xfs_da_args_t *args, /* operation arguments */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 374 | struct xfs_buf *dbp) /* input block's buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | { |
Nathan Scott | 68b3a10 | 2006-03-17 17:27:19 +1100 | [diff] [blame] | 376 | __be16 *bestsp; /* leaf's bestsp entries */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | xfs_dablk_t blkno; /* leaf block's bno */ |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 378 | xfs_dir2_data_hdr_t *hdr; /* block header */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | xfs_dir2_leaf_entry_t *blp; /* block's leaf entries */ |
| 380 | xfs_dir2_block_tail_t *btp; /* block's tail */ |
| 381 | xfs_inode_t *dp; /* incore directory inode */ |
| 382 | int error; /* error return code */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 383 | struct xfs_buf *lbp; /* leaf block's buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | xfs_dir2_db_t ldb; /* leaf block's bno */ |
| 385 | xfs_dir2_leaf_t *leaf; /* leaf structure */ |
| 386 | xfs_dir2_leaf_tail_t *ltp; /* leaf's tail */ |
| 387 | xfs_mount_t *mp; /* filesystem mount point */ |
| 388 | int needlog; /* need to log block header */ |
| 389 | int needscan; /* need to rescan bestfree */ |
| 390 | xfs_trans_t *tp; /* transaction pointer */ |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame] | 391 | struct xfs_dir2_data_free *bf; |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 392 | struct xfs_dir2_leaf_entry *ents; |
| 393 | struct xfs_dir3_icleaf_hdr leafhdr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 395 | trace_xfs_dir2_block_to_leaf(args); |
| 396 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | dp = args->dp; |
| 398 | mp = dp->i_mount; |
| 399 | tp = args->trans; |
| 400 | /* |
| 401 | * Add the leaf block to the inode. |
| 402 | * This interface will only put blocks in the leaf/node range. |
| 403 | * Since that's empty now, we'll get the root (block 0 in range). |
| 404 | */ |
| 405 | if ((error = xfs_da_grow_inode(args, &blkno))) { |
| 406 | return error; |
| 407 | } |
Dave Chinner | 2998ab1 | 2014-06-06 15:07:53 +1000 | [diff] [blame] | 408 | ldb = xfs_dir2_da_to_db(args->geo, blkno); |
Dave Chinner | 3002803 | 2014-06-06 15:08:18 +1000 | [diff] [blame] | 409 | ASSERT(ldb == xfs_dir2_byte_to_db(args->geo, XFS_DIR2_LEAF_OFFSET)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | /* |
| 411 | * Initialize the leaf block, get a buffer for it. |
| 412 | */ |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 413 | error = xfs_dir3_leaf_get_buf(args, ldb, &lbp, XFS_DIR2_LEAF1_MAGIC); |
| 414 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | return error; |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 416 | |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 417 | leaf = lbp->b_addr; |
| 418 | hdr = dbp->b_addr; |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame] | 419 | xfs_dir3_data_check(dp, dbp); |
Dave Chinner | 8f66193 | 2014-06-06 15:15:59 +1000 | [diff] [blame] | 420 | btp = xfs_dir2_block_tail_p(args->geo, hdr); |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 421 | blp = xfs_dir2_block_leaf_p(btp); |
Dave Chinner | 2ca9877 | 2013-10-29 22:11:49 +1100 | [diff] [blame] | 422 | bf = dp->d_ops->data_bestfree_p(hdr); |
Dave Chinner | 4141956 | 2013-10-29 22:11:50 +1100 | [diff] [blame] | 423 | ents = dp->d_ops->leaf_ents_p(leaf); |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 424 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | /* |
| 426 | * Set the counts in the leaf header. |
| 427 | */ |
Dave Chinner | 01ba43b | 2013-10-29 22:11:52 +1100 | [diff] [blame] | 428 | dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf); |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 429 | leafhdr.count = be32_to_cpu(btp->count); |
| 430 | leafhdr.stale = be32_to_cpu(btp->stale); |
Dave Chinner | 01ba43b | 2013-10-29 22:11:52 +1100 | [diff] [blame] | 431 | dp->d_ops->leaf_hdr_to_disk(leaf, &leafhdr); |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame^] | 432 | xfs_dir3_leaf_log_header(args, lbp); |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 433 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | /* |
| 435 | * Could compact these but I think we always do the conversion |
| 436 | * after squeezing out stale entries. |
| 437 | */ |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 438 | memcpy(ents, blp, be32_to_cpu(btp->count) * sizeof(xfs_dir2_leaf_entry_t)); |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame^] | 439 | xfs_dir3_leaf_log_ents(args, lbp, 0, leafhdr.count - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | needscan = 0; |
| 441 | needlog = 1; |
| 442 | /* |
| 443 | * Make the space formerly occupied by the leaf entries and block |
| 444 | * tail be free. |
| 445 | */ |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame^] | 446 | xfs_dir2_data_make_free(args, dbp, |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 447 | (xfs_dir2_data_aoff_t)((char *)blp - (char *)hdr), |
Dave Chinner | 8f66193 | 2014-06-06 15:15:59 +1000 | [diff] [blame] | 448 | (xfs_dir2_data_aoff_t)((char *)hdr + args->geo->blksize - |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | (char *)blp), |
| 450 | &needlog, &needscan); |
| 451 | /* |
| 452 | * Fix up the block header, make it a data block. |
| 453 | */ |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame] | 454 | dbp->b_ops = &xfs_dir3_data_buf_ops; |
Dave Chinner | 61fe135 | 2013-04-03 16:11:30 +1100 | [diff] [blame] | 455 | xfs_trans_buf_set_type(tp, dbp, XFS_BLFT_DIR_DATA_BUF); |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame] | 456 | if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC)) |
| 457 | hdr->magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC); |
| 458 | else |
| 459 | hdr->magic = cpu_to_be32(XFS_DIR3_DATA_MAGIC); |
| 460 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | if (needscan) |
Dave Chinner | 9d23fc8 | 2013-10-29 22:11:48 +1100 | [diff] [blame] | 462 | xfs_dir2_data_freescan(dp, hdr, &needlog); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | /* |
| 464 | * Set up leaf tail and bests table. |
| 465 | */ |
Dave Chinner | 8f66193 | 2014-06-06 15:15:59 +1000 | [diff] [blame] | 466 | ltp = xfs_dir2_leaf_tail_p(args->geo, leaf); |
Nathan Scott | afbcb3f | 2006-03-17 17:27:28 +1100 | [diff] [blame] | 467 | ltp->bestcount = cpu_to_be32(1); |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 468 | bestsp = xfs_dir2_leaf_bests_p(ltp); |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 469 | bestsp[0] = bf[0].length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | /* |
| 471 | * Log the data header and leaf bests table. |
| 472 | */ |
| 473 | if (needlog) |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame^] | 474 | xfs_dir2_data_log_header(args, dbp); |
Dave Chinner | 4141956 | 2013-10-29 22:11:50 +1100 | [diff] [blame] | 475 | xfs_dir3_leaf_check(dp, lbp); |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame] | 476 | xfs_dir3_data_check(dp, dbp); |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame^] | 477 | xfs_dir3_leaf_log_bests(args, lbp, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | return 0; |
| 479 | } |
| 480 | |
Christoph Hellwig | a230a1d | 2011-07-13 13:43:48 +0200 | [diff] [blame] | 481 | STATIC void |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 482 | xfs_dir3_leaf_find_stale( |
| 483 | struct xfs_dir3_icleaf_hdr *leafhdr, |
| 484 | struct xfs_dir2_leaf_entry *ents, |
Christoph Hellwig | a230a1d | 2011-07-13 13:43:48 +0200 | [diff] [blame] | 485 | int index, |
| 486 | int *lowstale, |
| 487 | int *highstale) |
| 488 | { |
| 489 | /* |
| 490 | * Find the first stale entry before our index, if any. |
| 491 | */ |
| 492 | for (*lowstale = index - 1; *lowstale >= 0; --*lowstale) { |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 493 | if (ents[*lowstale].address == |
Christoph Hellwig | a230a1d | 2011-07-13 13:43:48 +0200 | [diff] [blame] | 494 | cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) |
| 495 | break; |
| 496 | } |
| 497 | |
| 498 | /* |
| 499 | * Find the first stale entry at or after our index, if any. |
| 500 | * Stop if the result would require moving more entries than using |
| 501 | * lowstale. |
| 502 | */ |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 503 | for (*highstale = index; *highstale < leafhdr->count; ++*highstale) { |
| 504 | if (ents[*highstale].address == |
Christoph Hellwig | a230a1d | 2011-07-13 13:43:48 +0200 | [diff] [blame] | 505 | cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) |
| 506 | break; |
| 507 | if (*lowstale >= 0 && index - *lowstale <= *highstale - index) |
| 508 | break; |
| 509 | } |
| 510 | } |
| 511 | |
Christoph Hellwig | 4fb44c8 | 2011-07-08 14:34:59 +0200 | [diff] [blame] | 512 | struct xfs_dir2_leaf_entry * |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 513 | xfs_dir3_leaf_find_entry( |
| 514 | struct xfs_dir3_icleaf_hdr *leafhdr, |
| 515 | struct xfs_dir2_leaf_entry *ents, |
Christoph Hellwig | 4fb44c8 | 2011-07-08 14:34:59 +0200 | [diff] [blame] | 516 | int index, /* leaf table position */ |
| 517 | int compact, /* need to compact leaves */ |
| 518 | int lowstale, /* index of prev stale leaf */ |
| 519 | int highstale, /* index of next stale leaf */ |
| 520 | int *lfloglow, /* low leaf logging index */ |
| 521 | int *lfloghigh) /* high leaf logging index */ |
| 522 | { |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 523 | if (!leafhdr->stale) { |
Christoph Hellwig | 4fb44c8 | 2011-07-08 14:34:59 +0200 | [diff] [blame] | 524 | xfs_dir2_leaf_entry_t *lep; /* leaf entry table pointer */ |
| 525 | |
| 526 | /* |
| 527 | * Now we need to make room to insert the leaf entry. |
| 528 | * |
| 529 | * If there are no stale entries, just insert a hole at index. |
| 530 | */ |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 531 | lep = &ents[index]; |
| 532 | if (index < leafhdr->count) |
Christoph Hellwig | 4fb44c8 | 2011-07-08 14:34:59 +0200 | [diff] [blame] | 533 | memmove(lep + 1, lep, |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 534 | (leafhdr->count - index) * sizeof(*lep)); |
Christoph Hellwig | 4fb44c8 | 2011-07-08 14:34:59 +0200 | [diff] [blame] | 535 | |
| 536 | /* |
| 537 | * Record low and high logging indices for the leaf. |
| 538 | */ |
| 539 | *lfloglow = index; |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 540 | *lfloghigh = leafhdr->count++; |
Christoph Hellwig | 4fb44c8 | 2011-07-08 14:34:59 +0200 | [diff] [blame] | 541 | return lep; |
| 542 | } |
| 543 | |
| 544 | /* |
| 545 | * There are stale entries. |
| 546 | * |
| 547 | * We will use one of them for the new entry. It's probably not at |
| 548 | * the right location, so we'll have to shift some up or down first. |
| 549 | * |
| 550 | * If we didn't compact before, we need to find the nearest stale |
| 551 | * entries before and after our insertion point. |
| 552 | */ |
Christoph Hellwig | a230a1d | 2011-07-13 13:43:48 +0200 | [diff] [blame] | 553 | if (compact == 0) |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 554 | xfs_dir3_leaf_find_stale(leafhdr, ents, index, |
| 555 | &lowstale, &highstale); |
Christoph Hellwig | 4fb44c8 | 2011-07-08 14:34:59 +0200 | [diff] [blame] | 556 | |
| 557 | /* |
| 558 | * If the low one is better, use it. |
| 559 | */ |
| 560 | if (lowstale >= 0 && |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 561 | (highstale == leafhdr->count || |
Christoph Hellwig | 4fb44c8 | 2011-07-08 14:34:59 +0200 | [diff] [blame] | 562 | index - lowstale - 1 < highstale - index)) { |
| 563 | ASSERT(index - lowstale - 1 >= 0); |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 564 | ASSERT(ents[lowstale].address == |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 565 | cpu_to_be32(XFS_DIR2_NULL_DATAPTR)); |
Christoph Hellwig | 4fb44c8 | 2011-07-08 14:34:59 +0200 | [diff] [blame] | 566 | |
| 567 | /* |
| 568 | * Copy entries up to cover the stale entry and make room |
| 569 | * for the new entry. |
| 570 | */ |
| 571 | if (index - lowstale - 1 > 0) { |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 572 | memmove(&ents[lowstale], &ents[lowstale + 1], |
Christoph Hellwig | 4fb44c8 | 2011-07-08 14:34:59 +0200 | [diff] [blame] | 573 | (index - lowstale - 1) * |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 574 | sizeof(xfs_dir2_leaf_entry_t)); |
Christoph Hellwig | 4fb44c8 | 2011-07-08 14:34:59 +0200 | [diff] [blame] | 575 | } |
| 576 | *lfloglow = MIN(lowstale, *lfloglow); |
| 577 | *lfloghigh = MAX(index - 1, *lfloghigh); |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 578 | leafhdr->stale--; |
| 579 | return &ents[index - 1]; |
Christoph Hellwig | 4fb44c8 | 2011-07-08 14:34:59 +0200 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | /* |
| 583 | * The high one is better, so use that one. |
| 584 | */ |
| 585 | ASSERT(highstale - index >= 0); |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 586 | ASSERT(ents[highstale].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR)); |
Christoph Hellwig | 4fb44c8 | 2011-07-08 14:34:59 +0200 | [diff] [blame] | 587 | |
| 588 | /* |
| 589 | * Copy entries down to cover the stale entry and make room for the |
| 590 | * new entry. |
| 591 | */ |
| 592 | if (highstale - index > 0) { |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 593 | memmove(&ents[index + 1], &ents[index], |
Christoph Hellwig | 4fb44c8 | 2011-07-08 14:34:59 +0200 | [diff] [blame] | 594 | (highstale - index) * sizeof(xfs_dir2_leaf_entry_t)); |
| 595 | } |
| 596 | *lfloglow = MIN(index, *lfloglow); |
| 597 | *lfloghigh = MAX(highstale, *lfloghigh); |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 598 | leafhdr->stale--; |
| 599 | return &ents[index]; |
Christoph Hellwig | 4fb44c8 | 2011-07-08 14:34:59 +0200 | [diff] [blame] | 600 | } |
| 601 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | /* |
| 603 | * Add an entry to a leaf form directory. |
| 604 | */ |
| 605 | int /* error */ |
| 606 | xfs_dir2_leaf_addname( |
| 607 | xfs_da_args_t *args) /* operation arguments */ |
| 608 | { |
Nathan Scott | 68b3a10 | 2006-03-17 17:27:19 +1100 | [diff] [blame] | 609 | __be16 *bestsp; /* freespace table in leaf */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | int compact; /* need to compact leaves */ |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 611 | xfs_dir2_data_hdr_t *hdr; /* data block header */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 612 | struct xfs_buf *dbp; /* data block buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | xfs_dir2_data_entry_t *dep; /* data block entry */ |
| 614 | xfs_inode_t *dp; /* incore directory inode */ |
| 615 | xfs_dir2_data_unused_t *dup; /* data unused entry */ |
| 616 | int error; /* error return value */ |
| 617 | int grown; /* allocated new data block */ |
| 618 | int highstale; /* index of next stale leaf */ |
| 619 | int i; /* temporary, index */ |
| 620 | int index; /* leaf table position */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 621 | struct xfs_buf *lbp; /* leaf's buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | xfs_dir2_leaf_t *leaf; /* leaf structure */ |
| 623 | int length; /* length of new entry */ |
| 624 | xfs_dir2_leaf_entry_t *lep; /* leaf entry table pointer */ |
| 625 | int lfloglow; /* low leaf logging index */ |
| 626 | int lfloghigh; /* high leaf logging index */ |
| 627 | int lowstale; /* index of prev stale leaf */ |
| 628 | xfs_dir2_leaf_tail_t *ltp; /* leaf tail pointer */ |
| 629 | xfs_mount_t *mp; /* filesystem mount point */ |
| 630 | int needbytes; /* leaf block bytes needed */ |
| 631 | int needlog; /* need to log data header */ |
| 632 | int needscan; /* need to rescan data free */ |
Nathan Scott | 3d693c6 | 2006-03-17 17:28:27 +1100 | [diff] [blame] | 633 | __be16 *tagp; /* end of data entry */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | xfs_trans_t *tp; /* transaction pointer */ |
| 635 | xfs_dir2_db_t use_block; /* data block number */ |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame] | 636 | struct xfs_dir2_data_free *bf; /* bestfree table */ |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 637 | struct xfs_dir2_leaf_entry *ents; |
| 638 | struct xfs_dir3_icleaf_hdr leafhdr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 640 | trace_xfs_dir2_leaf_addname(args); |
| 641 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | dp = args->dp; |
| 643 | tp = args->trans; |
| 644 | mp = dp->i_mount; |
Dave Chinner | e6f7667 | 2012-11-12 22:54:15 +1100 | [diff] [blame] | 645 | |
Dave Chinner | 7dda6e8 | 2014-06-06 15:11:18 +1000 | [diff] [blame] | 646 | error = xfs_dir3_leaf_read(tp, dp, args->geo->leafblk, -1, &lbp); |
Dave Chinner | 4bb20a8 | 2012-11-12 22:54:10 +1100 | [diff] [blame] | 647 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | return error; |
Dave Chinner | e6f7667 | 2012-11-12 22:54:15 +1100 | [diff] [blame] | 649 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | /* |
| 651 | * Look up the entry by hash value and name. |
| 652 | * We know it's not there, our caller has already done a lookup. |
| 653 | * So the index is of the entry to insert in front of. |
| 654 | * But if there are dup hash values the index is of the first of those. |
| 655 | */ |
| 656 | index = xfs_dir2_leaf_search_hash(args, lbp); |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 657 | leaf = lbp->b_addr; |
Dave Chinner | 8f66193 | 2014-06-06 15:15:59 +1000 | [diff] [blame] | 658 | ltp = xfs_dir2_leaf_tail_p(args->geo, leaf); |
Dave Chinner | 4141956 | 2013-10-29 22:11:50 +1100 | [diff] [blame] | 659 | ents = dp->d_ops->leaf_ents_p(leaf); |
Dave Chinner | 01ba43b | 2013-10-29 22:11:52 +1100 | [diff] [blame] | 660 | dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf); |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 661 | bestsp = xfs_dir2_leaf_bests_p(ltp); |
Dave Chinner | 9d23fc8 | 2013-10-29 22:11:48 +1100 | [diff] [blame] | 662 | length = dp->d_ops->data_entsize(args->namelen); |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 663 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | /* |
| 665 | * See if there are any entries with the same hash value |
| 666 | * and space in their block for the new entry. |
| 667 | * This is good because it puts multiple same-hash value entries |
| 668 | * in a data block, improving the lookup of those entries. |
| 669 | */ |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 670 | for (use_block = -1, lep = &ents[index]; |
| 671 | index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | index++, lep++) { |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 673 | if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | continue; |
Dave Chinner | 3002803 | 2014-06-06 15:08:18 +1000 | [diff] [blame] | 675 | i = xfs_dir2_dataptr_to_db(args->geo, be32_to_cpu(lep->address)); |
Nathan Scott | afbcb3f | 2006-03-17 17:27:28 +1100 | [diff] [blame] | 676 | ASSERT(i < be32_to_cpu(ltp->bestcount)); |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 677 | ASSERT(bestsp[i] != cpu_to_be16(NULLDATAOFF)); |
Nathan Scott | 68b3a10 | 2006-03-17 17:27:19 +1100 | [diff] [blame] | 678 | if (be16_to_cpu(bestsp[i]) >= length) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | use_block = i; |
| 680 | break; |
| 681 | } |
| 682 | } |
| 683 | /* |
| 684 | * Didn't find a block yet, linear search all the data blocks. |
| 685 | */ |
| 686 | if (use_block == -1) { |
Nathan Scott | afbcb3f | 2006-03-17 17:27:28 +1100 | [diff] [blame] | 687 | for (i = 0; i < be32_to_cpu(ltp->bestcount); i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | /* |
| 689 | * Remember a block we see that's missing. |
| 690 | */ |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 691 | if (bestsp[i] == cpu_to_be16(NULLDATAOFF) && |
| 692 | use_block == -1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | use_block = i; |
Nathan Scott | 68b3a10 | 2006-03-17 17:27:19 +1100 | [diff] [blame] | 694 | else if (be16_to_cpu(bestsp[i]) >= length) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | use_block = i; |
| 696 | break; |
| 697 | } |
| 698 | } |
| 699 | } |
| 700 | /* |
| 701 | * How many bytes do we need in the leaf block? |
| 702 | */ |
Christoph Hellwig | 2282396 | 2011-07-08 14:35:53 +0200 | [diff] [blame] | 703 | needbytes = 0; |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 704 | if (!leafhdr.stale) |
Christoph Hellwig | 2282396 | 2011-07-08 14:35:53 +0200 | [diff] [blame] | 705 | needbytes += sizeof(xfs_dir2_leaf_entry_t); |
| 706 | if (use_block == -1) |
| 707 | needbytes += sizeof(xfs_dir2_data_off_t); |
| 708 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | /* |
| 710 | * Now kill use_block if it refers to a missing block, so we |
| 711 | * can use it as an indication of allocation needed. |
| 712 | */ |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 713 | if (use_block != -1 && bestsp[use_block] == cpu_to_be16(NULLDATAOFF)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | use_block = -1; |
| 715 | /* |
| 716 | * If we don't have enough free bytes but we can make enough |
| 717 | * by compacting out stale entries, we'll do that. |
| 718 | */ |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 719 | if ((char *)bestsp - (char *)&ents[leafhdr.count] < needbytes && |
| 720 | leafhdr.stale > 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | compact = 1; |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 722 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | /* |
| 724 | * Otherwise if we don't have enough free bytes we need to |
| 725 | * convert to node form. |
| 726 | */ |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 727 | else if ((char *)bestsp - (char *)&ents[leafhdr.count] < needbytes) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | /* |
| 729 | * Just checking or no space reservation, give up. |
| 730 | */ |
Barry Naujok | 6a17810 | 2008-05-21 16:42:05 +1000 | [diff] [blame] | 731 | if ((args->op_flags & XFS_DA_OP_JUSTCHECK) || |
| 732 | args->total == 0) { |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 733 | xfs_trans_brelse(tp, lbp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 | return XFS_ERROR(ENOSPC); |
| 735 | } |
| 736 | /* |
| 737 | * Convert to node form. |
| 738 | */ |
| 739 | error = xfs_dir2_leaf_to_node(args, lbp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | if (error) |
| 741 | return error; |
| 742 | /* |
| 743 | * Then add the new entry. |
| 744 | */ |
| 745 | return xfs_dir2_node_addname(args); |
| 746 | } |
| 747 | /* |
| 748 | * Otherwise it will fit without compaction. |
| 749 | */ |
| 750 | else |
| 751 | compact = 0; |
| 752 | /* |
| 753 | * If just checking, then it will fit unless we needed to allocate |
| 754 | * a new data block. |
| 755 | */ |
Barry Naujok | 6a17810 | 2008-05-21 16:42:05 +1000 | [diff] [blame] | 756 | if (args->op_flags & XFS_DA_OP_JUSTCHECK) { |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 757 | xfs_trans_brelse(tp, lbp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | return use_block == -1 ? XFS_ERROR(ENOSPC) : 0; |
| 759 | } |
| 760 | /* |
| 761 | * If no allocations are allowed, return now before we've |
| 762 | * changed anything. |
| 763 | */ |
| 764 | if (args->total == 0 && use_block == -1) { |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 765 | xfs_trans_brelse(tp, lbp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | return XFS_ERROR(ENOSPC); |
| 767 | } |
| 768 | /* |
| 769 | * Need to compact the leaf entries, removing stale ones. |
| 770 | * Leave one stale entry behind - the one closest to our |
| 771 | * insertion index - and we'll shift that one to our insertion |
| 772 | * point later. |
| 773 | */ |
| 774 | if (compact) { |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 775 | xfs_dir3_leaf_compact_x1(&leafhdr, ents, &index, &lowstale, |
| 776 | &highstale, &lfloglow, &lfloghigh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | } |
| 778 | /* |
| 779 | * There are stale entries, so we'll need log-low and log-high |
| 780 | * impossibly bad values later. |
| 781 | */ |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 782 | else if (leafhdr.stale) { |
| 783 | lfloglow = leafhdr.count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | lfloghigh = -1; |
| 785 | } |
| 786 | /* |
| 787 | * If there was no data block space found, we need to allocate |
| 788 | * a new one. |
| 789 | */ |
| 790 | if (use_block == -1) { |
| 791 | /* |
| 792 | * Add the new data block. |
| 793 | */ |
| 794 | if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE, |
| 795 | &use_block))) { |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 796 | xfs_trans_brelse(tp, lbp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | return error; |
| 798 | } |
| 799 | /* |
| 800 | * Initialize the block. |
| 801 | */ |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 802 | if ((error = xfs_dir3_data_init(args, use_block, &dbp))) { |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 803 | xfs_trans_brelse(tp, lbp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | return error; |
| 805 | } |
| 806 | /* |
| 807 | * If we're adding a new data block on the end we need to |
| 808 | * extend the bests table. Copy it up one entry. |
| 809 | */ |
Nathan Scott | afbcb3f | 2006-03-17 17:27:28 +1100 | [diff] [blame] | 810 | if (use_block >= be32_to_cpu(ltp->bestcount)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | bestsp--; |
| 812 | memmove(&bestsp[0], &bestsp[1], |
Nathan Scott | afbcb3f | 2006-03-17 17:27:28 +1100 | [diff] [blame] | 813 | be32_to_cpu(ltp->bestcount) * sizeof(bestsp[0])); |
Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 814 | be32_add_cpu(<p->bestcount, 1); |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame^] | 815 | xfs_dir3_leaf_log_tail(args, lbp); |
| 816 | xfs_dir3_leaf_log_bests(args, lbp, 0, |
| 817 | be32_to_cpu(ltp->bestcount) - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | } |
| 819 | /* |
| 820 | * If we're filling in a previously empty block just log it. |
| 821 | */ |
| 822 | else |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame^] | 823 | xfs_dir3_leaf_log_bests(args, lbp, use_block, use_block); |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 824 | hdr = dbp->b_addr; |
Dave Chinner | 2ca9877 | 2013-10-29 22:11:49 +1100 | [diff] [blame] | 825 | bf = dp->d_ops->data_bestfree_p(hdr); |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame] | 826 | bestsp[use_block] = bf[0].length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | grown = 1; |
Dave Chinner | e481357 | 2012-11-12 22:54:14 +1100 | [diff] [blame] | 828 | } else { |
| 829 | /* |
| 830 | * Already had space in some data block. |
| 831 | * Just read that one in. |
| 832 | */ |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame] | 833 | error = xfs_dir3_data_read(tp, dp, |
Dave Chinner | 2998ab1 | 2014-06-06 15:07:53 +1000 | [diff] [blame] | 834 | xfs_dir2_db_to_da(args->geo, use_block), |
| 835 | -1, &dbp); |
Dave Chinner | 4bb20a8 | 2012-11-12 22:54:10 +1100 | [diff] [blame] | 836 | if (error) { |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 837 | xfs_trans_brelse(tp, lbp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | return error; |
| 839 | } |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 840 | hdr = dbp->b_addr; |
Dave Chinner | 2ca9877 | 2013-10-29 22:11:49 +1100 | [diff] [blame] | 841 | bf = dp->d_ops->data_bestfree_p(hdr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 | grown = 0; |
| 843 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | /* |
| 845 | * Point to the biggest freespace in our data block. |
| 846 | */ |
| 847 | dup = (xfs_dir2_data_unused_t *) |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame] | 848 | ((char *)hdr + be16_to_cpu(bf[0].offset)); |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 849 | ASSERT(be16_to_cpu(dup->length) >= length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 850 | needscan = needlog = 0; |
| 851 | /* |
| 852 | * Mark the initial part of our freespace in use for the new entry. |
| 853 | */ |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame^] | 854 | xfs_dir2_data_use_free(args, dbp, dup, |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 855 | (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr), length, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | &needlog, &needscan); |
| 857 | /* |
| 858 | * Initialize our new entry (at last). |
| 859 | */ |
| 860 | dep = (xfs_dir2_data_entry_t *)dup; |
Christoph Hellwig | ff9901c | 2006-06-09 14:48:37 +1000 | [diff] [blame] | 861 | dep->inumber = cpu_to_be64(args->inumber); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | dep->namelen = args->namelen; |
| 863 | memcpy(dep->name, args->name, dep->namelen); |
Dave Chinner | 9d23fc8 | 2013-10-29 22:11:48 +1100 | [diff] [blame] | 864 | dp->d_ops->data_put_ftype(dep, args->filetype); |
| 865 | tagp = dp->d_ops->data_entry_tag_p(dep); |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 866 | *tagp = cpu_to_be16((char *)dep - (char *)hdr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | /* |
| 868 | * Need to scan fix up the bestfree table. |
| 869 | */ |
| 870 | if (needscan) |
Dave Chinner | 9d23fc8 | 2013-10-29 22:11:48 +1100 | [diff] [blame] | 871 | xfs_dir2_data_freescan(dp, hdr, &needlog); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | /* |
| 873 | * Need to log the data block's header. |
| 874 | */ |
| 875 | if (needlog) |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame^] | 876 | xfs_dir2_data_log_header(args, dbp); |
| 877 | xfs_dir2_data_log_entry(args, dbp, dep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | /* |
| 879 | * If the bests table needs to be changed, do it. |
| 880 | * Log the change unless we've already done that. |
| 881 | */ |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame] | 882 | if (be16_to_cpu(bestsp[use_block]) != be16_to_cpu(bf[0].length)) { |
| 883 | bestsp[use_block] = bf[0].length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | if (!grown) |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame^] | 885 | xfs_dir3_leaf_log_bests(args, lbp, use_block, use_block); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | } |
Christoph Hellwig | 4fb44c8 | 2011-07-08 14:34:59 +0200 | [diff] [blame] | 887 | |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 888 | lep = xfs_dir3_leaf_find_entry(&leafhdr, ents, index, compact, lowstale, |
Christoph Hellwig | 4fb44c8 | 2011-07-08 14:34:59 +0200 | [diff] [blame] | 889 | highstale, &lfloglow, &lfloghigh); |
| 890 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | /* |
| 892 | * Fill in the new leaf entry. |
| 893 | */ |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 894 | lep->hashval = cpu_to_be32(args->hashval); |
Dave Chinner | 3002803 | 2014-06-06 15:08:18 +1000 | [diff] [blame] | 895 | lep->address = cpu_to_be32( |
| 896 | xfs_dir2_db_off_to_dataptr(args->geo, use_block, |
Nathan Scott | 3d693c6 | 2006-03-17 17:28:27 +1100 | [diff] [blame] | 897 | be16_to_cpu(*tagp))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | /* |
| 899 | * Log the leaf fields and give up the buffers. |
| 900 | */ |
Dave Chinner | 01ba43b | 2013-10-29 22:11:52 +1100 | [diff] [blame] | 901 | dp->d_ops->leaf_hdr_to_disk(leaf, &leafhdr); |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame^] | 902 | xfs_dir3_leaf_log_header(args, lbp); |
| 903 | xfs_dir3_leaf_log_ents(args, lbp, lfloglow, lfloghigh); |
Dave Chinner | 4141956 | 2013-10-29 22:11:50 +1100 | [diff] [blame] | 904 | xfs_dir3_leaf_check(dp, lbp); |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame] | 905 | xfs_dir3_data_check(dp, dbp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | return 0; |
| 907 | } |
| 908 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 909 | /* |
| 910 | * Compact out any stale entries in the leaf. |
| 911 | * Log the header and changed leaf entries, if any. |
| 912 | */ |
| 913 | void |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 914 | xfs_dir3_leaf_compact( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | xfs_da_args_t *args, /* operation arguments */ |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 916 | struct xfs_dir3_icleaf_hdr *leafhdr, |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 917 | struct xfs_buf *bp) /* leaf buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | { |
| 919 | int from; /* source leaf index */ |
| 920 | xfs_dir2_leaf_t *leaf; /* leaf structure */ |
| 921 | int loglow; /* first leaf entry to log */ |
| 922 | int to; /* target leaf index */ |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 923 | struct xfs_dir2_leaf_entry *ents; |
Dave Chinner | 01ba43b | 2013-10-29 22:11:52 +1100 | [diff] [blame] | 924 | struct xfs_inode *dp = args->dp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 926 | leaf = bp->b_addr; |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 927 | if (!leafhdr->stale) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | return; |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 929 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | /* |
| 931 | * Compress out the stale entries in place. |
| 932 | */ |
Dave Chinner | 01ba43b | 2013-10-29 22:11:52 +1100 | [diff] [blame] | 933 | ents = dp->d_ops->leaf_ents_p(leaf); |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 934 | for (from = to = 0, loglow = -1; from < leafhdr->count; from++) { |
| 935 | if (ents[from].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 936 | continue; |
| 937 | /* |
| 938 | * Only actually copy the entries that are different. |
| 939 | */ |
| 940 | if (from > to) { |
| 941 | if (loglow == -1) |
| 942 | loglow = to; |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 943 | ents[to] = ents[from]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | } |
| 945 | to++; |
| 946 | } |
| 947 | /* |
| 948 | * Update and log the header, log the leaf entries. |
| 949 | */ |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 950 | ASSERT(leafhdr->stale == from - to); |
| 951 | leafhdr->count -= leafhdr->stale; |
| 952 | leafhdr->stale = 0; |
| 953 | |
Dave Chinner | 01ba43b | 2013-10-29 22:11:52 +1100 | [diff] [blame] | 954 | dp->d_ops->leaf_hdr_to_disk(leaf, leafhdr); |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame^] | 955 | xfs_dir3_leaf_log_header(args, bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 956 | if (loglow != -1) |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame^] | 957 | xfs_dir3_leaf_log_ents(args, bp, loglow, to - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | } |
| 959 | |
| 960 | /* |
| 961 | * Compact the leaf entries, removing stale ones. |
| 962 | * Leave one stale entry behind - the one closest to our |
| 963 | * insertion index - and the caller will shift that one to our insertion |
| 964 | * point later. |
| 965 | * Return new insertion index, where the remaining stale entry is, |
| 966 | * and leaf logging indices. |
| 967 | */ |
| 968 | void |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 969 | xfs_dir3_leaf_compact_x1( |
| 970 | struct xfs_dir3_icleaf_hdr *leafhdr, |
| 971 | struct xfs_dir2_leaf_entry *ents, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | int *indexp, /* insertion index */ |
| 973 | int *lowstalep, /* out: stale entry before us */ |
| 974 | int *highstalep, /* out: stale entry after us */ |
| 975 | int *lowlogp, /* out: low log index */ |
| 976 | int *highlogp) /* out: high log index */ |
| 977 | { |
| 978 | int from; /* source copy index */ |
| 979 | int highstale; /* stale entry at/after index */ |
| 980 | int index; /* insertion index */ |
| 981 | int keepstale; /* source index of kept stale */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 982 | int lowstale; /* stale entry before index */ |
| 983 | int newindex=0; /* new insertion index */ |
| 984 | int to; /* destination copy index */ |
| 985 | |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 986 | ASSERT(leafhdr->stale > 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 987 | index = *indexp; |
Christoph Hellwig | a230a1d | 2011-07-13 13:43:48 +0200 | [diff] [blame] | 988 | |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 989 | xfs_dir3_leaf_find_stale(leafhdr, ents, index, &lowstale, &highstale); |
Christoph Hellwig | a230a1d | 2011-07-13 13:43:48 +0200 | [diff] [blame] | 990 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | /* |
| 992 | * Pick the better of lowstale and highstale. |
| 993 | */ |
| 994 | if (lowstale >= 0 && |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 995 | (highstale == leafhdr->count || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 996 | index - lowstale <= highstale - index)) |
| 997 | keepstale = lowstale; |
| 998 | else |
| 999 | keepstale = highstale; |
| 1000 | /* |
| 1001 | * Copy the entries in place, removing all the stale entries |
| 1002 | * except keepstale. |
| 1003 | */ |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1004 | for (from = to = 0; from < leafhdr->count; from++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | /* |
| 1006 | * Notice the new value of index. |
| 1007 | */ |
| 1008 | if (index == from) |
| 1009 | newindex = to; |
| 1010 | if (from != keepstale && |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1011 | ents[from].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1012 | if (from == to) |
| 1013 | *lowlogp = to; |
| 1014 | continue; |
| 1015 | } |
| 1016 | /* |
| 1017 | * Record the new keepstale value for the insertion. |
| 1018 | */ |
| 1019 | if (from == keepstale) |
| 1020 | lowstale = highstale = to; |
| 1021 | /* |
| 1022 | * Copy only the entries that have moved. |
| 1023 | */ |
| 1024 | if (from > to) |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1025 | ents[to] = ents[from]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1026 | to++; |
| 1027 | } |
| 1028 | ASSERT(from > to); |
| 1029 | /* |
| 1030 | * If the insertion point was past the last entry, |
| 1031 | * set the new insertion point accordingly. |
| 1032 | */ |
| 1033 | if (index == from) |
| 1034 | newindex = to; |
| 1035 | *indexp = newindex; |
| 1036 | /* |
| 1037 | * Adjust the leaf header values. |
| 1038 | */ |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1039 | leafhdr->count -= from - to; |
| 1040 | leafhdr->stale = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1041 | /* |
| 1042 | * Remember the low/high stale value only in the "right" |
| 1043 | * direction. |
| 1044 | */ |
| 1045 | if (lowstale >= newindex) |
| 1046 | lowstale = -1; |
| 1047 | else |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1048 | highstale = leafhdr->count; |
| 1049 | *highlogp = leafhdr->count - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1050 | *lowstalep = lowstale; |
| 1051 | *highstalep = highstale; |
| 1052 | } |
| 1053 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1054 | /* |
| 1055 | * Log the bests entries indicated from a leaf1 block. |
| 1056 | */ |
Christoph Hellwig | ba0f32d | 2005-06-21 15:36:52 +1000 | [diff] [blame] | 1057 | static void |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1058 | xfs_dir3_leaf_log_bests( |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame^] | 1059 | struct xfs_da_args *args, |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1060 | struct xfs_buf *bp, /* leaf buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1061 | int first, /* first entry to log */ |
| 1062 | int last) /* last entry to log */ |
| 1063 | { |
Nathan Scott | 68b3a10 | 2006-03-17 17:27:19 +1100 | [diff] [blame] | 1064 | __be16 *firstb; /* pointer to first entry */ |
| 1065 | __be16 *lastb; /* pointer to last entry */ |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1066 | struct xfs_dir2_leaf *leaf = bp->b_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */ |
| 1068 | |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1069 | ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) || |
| 1070 | leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC)); |
| 1071 | |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame^] | 1072 | ltp = xfs_dir2_leaf_tail_p(args->geo, leaf); |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1073 | firstb = xfs_dir2_leaf_bests_p(ltp) + first; |
| 1074 | lastb = xfs_dir2_leaf_bests_p(ltp) + last; |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame^] | 1075 | xfs_trans_log_buf(args->trans, bp, |
| 1076 | (uint)((char *)firstb - (char *)leaf), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | (uint)((char *)lastb - (char *)leaf + sizeof(*lastb) - 1)); |
| 1078 | } |
| 1079 | |
| 1080 | /* |
| 1081 | * Log the leaf entries indicated from a leaf1 or leafn block. |
| 1082 | */ |
| 1083 | void |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1084 | xfs_dir3_leaf_log_ents( |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame^] | 1085 | struct xfs_da_args *args, |
Dave Chinner | 4141956 | 2013-10-29 22:11:50 +1100 | [diff] [blame] | 1086 | struct xfs_buf *bp, |
| 1087 | int first, |
| 1088 | int last) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1089 | { |
| 1090 | xfs_dir2_leaf_entry_t *firstlep; /* pointer to first entry */ |
| 1091 | xfs_dir2_leaf_entry_t *lastlep; /* pointer to last entry */ |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1092 | struct xfs_dir2_leaf *leaf = bp->b_addr; |
| 1093 | struct xfs_dir2_leaf_entry *ents; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1094 | |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 1095 | ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) || |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1096 | leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) || |
| 1097 | leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) || |
| 1098 | leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC)); |
| 1099 | |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame^] | 1100 | ents = args->dp->d_ops->leaf_ents_p(leaf); |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1101 | firstlep = &ents[first]; |
| 1102 | lastlep = &ents[last]; |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame^] | 1103 | xfs_trans_log_buf(args->trans, bp, |
| 1104 | (uint)((char *)firstlep - (char *)leaf), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1105 | (uint)((char *)lastlep - (char *)leaf + sizeof(*lastlep) - 1)); |
| 1106 | } |
| 1107 | |
| 1108 | /* |
| 1109 | * Log the header of the leaf1 or leafn block. |
| 1110 | */ |
| 1111 | void |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1112 | xfs_dir3_leaf_log_header( |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame^] | 1113 | struct xfs_da_args *args, |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1114 | struct xfs_buf *bp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1115 | { |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1116 | struct xfs_dir2_leaf *leaf = bp->b_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1117 | |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 1118 | ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) || |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1119 | leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) || |
| 1120 | leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) || |
| 1121 | leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC)); |
| 1122 | |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame^] | 1123 | xfs_trans_log_buf(args->trans, bp, |
| 1124 | (uint)((char *)&leaf->hdr - (char *)leaf), |
| 1125 | args->dp->d_ops->leaf_hdr_size - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1126 | } |
| 1127 | |
| 1128 | /* |
| 1129 | * Log the tail of the leaf1 block. |
| 1130 | */ |
Christoph Hellwig | ba0f32d | 2005-06-21 15:36:52 +1000 | [diff] [blame] | 1131 | STATIC void |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1132 | xfs_dir3_leaf_log_tail( |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame^] | 1133 | struct xfs_da_args *args, |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1134 | struct xfs_buf *bp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1135 | { |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1136 | struct xfs_dir2_leaf *leaf = bp->b_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1137 | xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1138 | |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1139 | ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) || |
| 1140 | leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) || |
| 1141 | leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) || |
| 1142 | leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC)); |
| 1143 | |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame^] | 1144 | ltp = xfs_dir2_leaf_tail_p(args->geo, leaf); |
| 1145 | xfs_trans_log_buf(args->trans, bp, (uint)((char *)ltp - (char *)leaf), |
| 1146 | (uint)(args->geo->blksize - 1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1147 | } |
| 1148 | |
| 1149 | /* |
| 1150 | * Look up the entry referred to by args in the leaf format directory. |
| 1151 | * Most of the work is done by the xfs_dir2_leaf_lookup_int routine which |
| 1152 | * is also used by the node-format code. |
| 1153 | */ |
| 1154 | int |
| 1155 | xfs_dir2_leaf_lookup( |
| 1156 | xfs_da_args_t *args) /* operation arguments */ |
| 1157 | { |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1158 | struct xfs_buf *dbp; /* data block buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1159 | xfs_dir2_data_entry_t *dep; /* data block entry */ |
| 1160 | xfs_inode_t *dp; /* incore directory inode */ |
| 1161 | int error; /* error return code */ |
| 1162 | int index; /* found entry index */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1163 | struct xfs_buf *lbp; /* leaf buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1164 | xfs_dir2_leaf_t *leaf; /* leaf structure */ |
| 1165 | xfs_dir2_leaf_entry_t *lep; /* leaf entry */ |
| 1166 | xfs_trans_t *tp; /* transaction pointer */ |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1167 | struct xfs_dir2_leaf_entry *ents; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1168 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1169 | trace_xfs_dir2_leaf_lookup(args); |
| 1170 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1171 | /* |
| 1172 | * Look up name in the leaf block, returning both buffers and index. |
| 1173 | */ |
| 1174 | if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) { |
| 1175 | return error; |
| 1176 | } |
| 1177 | tp = args->trans; |
| 1178 | dp = args->dp; |
Dave Chinner | 4141956 | 2013-10-29 22:11:50 +1100 | [diff] [blame] | 1179 | xfs_dir3_leaf_check(dp, lbp); |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1180 | leaf = lbp->b_addr; |
Dave Chinner | 4141956 | 2013-10-29 22:11:50 +1100 | [diff] [blame] | 1181 | ents = dp->d_ops->leaf_ents_p(leaf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1182 | /* |
| 1183 | * Get to the leaf entry and contained data entry address. |
| 1184 | */ |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1185 | lep = &ents[index]; |
| 1186 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1187 | /* |
| 1188 | * Point to the data entry. |
| 1189 | */ |
| 1190 | dep = (xfs_dir2_data_entry_t *) |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1191 | ((char *)dbp->b_addr + |
Dave Chinner | 3002803 | 2014-06-06 15:08:18 +1000 | [diff] [blame] | 1192 | xfs_dir2_dataptr_to_off(args->geo, be32_to_cpu(lep->address))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1193 | /* |
Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame] | 1194 | * Return the found inode number & CI name if appropriate |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1195 | */ |
Christoph Hellwig | ff9901c | 2006-06-09 14:48:37 +1000 | [diff] [blame] | 1196 | args->inumber = be64_to_cpu(dep->inumber); |
Dave Chinner | 9d23fc8 | 2013-10-29 22:11:48 +1100 | [diff] [blame] | 1197 | args->filetype = dp->d_ops->data_get_ftype(dep); |
Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame] | 1198 | error = xfs_dir_cilookup_result(args, dep->name, dep->namelen); |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1199 | xfs_trans_brelse(tp, dbp); |
| 1200 | xfs_trans_brelse(tp, lbp); |
Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame] | 1201 | return XFS_ERROR(error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 | } |
| 1203 | |
| 1204 | /* |
| 1205 | * Look up name/hash in the leaf block. |
| 1206 | * Fill in indexp with the found index, and dbpp with the data buffer. |
| 1207 | * If not found dbpp will be NULL, and ENOENT comes back. |
| 1208 | * lbpp will always be filled in with the leaf buffer unless there's an error. |
| 1209 | */ |
| 1210 | static int /* error */ |
| 1211 | xfs_dir2_leaf_lookup_int( |
| 1212 | xfs_da_args_t *args, /* operation arguments */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1213 | struct xfs_buf **lbpp, /* out: leaf buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1214 | int *indexp, /* out: index in leaf block */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1215 | struct xfs_buf **dbpp) /* out: data buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1216 | { |
Barry Naujok | 07fe4dd | 2008-06-27 13:32:11 +1000 | [diff] [blame] | 1217 | xfs_dir2_db_t curdb = -1; /* current data block number */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1218 | struct xfs_buf *dbp = NULL; /* data buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1219 | xfs_dir2_data_entry_t *dep; /* data entry */ |
| 1220 | xfs_inode_t *dp; /* incore directory inode */ |
| 1221 | int error; /* error return code */ |
| 1222 | int index; /* index in leaf block */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1223 | struct xfs_buf *lbp; /* leaf buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1224 | xfs_dir2_leaf_entry_t *lep; /* leaf entry */ |
| 1225 | xfs_dir2_leaf_t *leaf; /* leaf structure */ |
| 1226 | xfs_mount_t *mp; /* filesystem mount point */ |
| 1227 | xfs_dir2_db_t newdb; /* new data block number */ |
| 1228 | xfs_trans_t *tp; /* transaction pointer */ |
Barry Naujok | 07fe4dd | 2008-06-27 13:32:11 +1000 | [diff] [blame] | 1229 | xfs_dir2_db_t cidb = -1; /* case match data block no. */ |
Barry Naujok | 5163f95 | 2008-05-21 16:41:01 +1000 | [diff] [blame] | 1230 | enum xfs_dacmp cmp; /* name compare result */ |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1231 | struct xfs_dir2_leaf_entry *ents; |
| 1232 | struct xfs_dir3_icleaf_hdr leafhdr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1233 | |
| 1234 | dp = args->dp; |
| 1235 | tp = args->trans; |
| 1236 | mp = dp->i_mount; |
Dave Chinner | e6f7667 | 2012-11-12 22:54:15 +1100 | [diff] [blame] | 1237 | |
Dave Chinner | 7dda6e8 | 2014-06-06 15:11:18 +1000 | [diff] [blame] | 1238 | error = xfs_dir3_leaf_read(tp, dp, args->geo->leafblk, -1, &lbp); |
Barry Naujok | 07fe4dd | 2008-06-27 13:32:11 +1000 | [diff] [blame] | 1239 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1240 | return error; |
Dave Chinner | e6f7667 | 2012-11-12 22:54:15 +1100 | [diff] [blame] | 1241 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1242 | *lbpp = lbp; |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1243 | leaf = lbp->b_addr; |
Dave Chinner | 4141956 | 2013-10-29 22:11:50 +1100 | [diff] [blame] | 1244 | xfs_dir3_leaf_check(dp, lbp); |
| 1245 | ents = dp->d_ops->leaf_ents_p(leaf); |
Dave Chinner | 01ba43b | 2013-10-29 22:11:52 +1100 | [diff] [blame] | 1246 | dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf); |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1247 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1248 | /* |
| 1249 | * Look for the first leaf entry with our hash value. |
| 1250 | */ |
| 1251 | index = xfs_dir2_leaf_search_hash(args, lbp); |
| 1252 | /* |
| 1253 | * Loop over all the entries with the right hash value |
| 1254 | * looking to match the name. |
| 1255 | */ |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1256 | for (lep = &ents[index]; |
| 1257 | index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval; |
| 1258 | lep++, index++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1259 | /* |
| 1260 | * Skip over stale leaf entries. |
| 1261 | */ |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 1262 | if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1263 | continue; |
| 1264 | /* |
| 1265 | * Get the new data block number. |
| 1266 | */ |
Dave Chinner | 3002803 | 2014-06-06 15:08:18 +1000 | [diff] [blame] | 1267 | newdb = xfs_dir2_dataptr_to_db(args->geo, |
| 1268 | be32_to_cpu(lep->address)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1269 | /* |
| 1270 | * If it's not the same as the old data block number, |
| 1271 | * need to pitch the old one and read the new one. |
| 1272 | */ |
| 1273 | if (newdb != curdb) { |
Barry Naujok | 07fe4dd | 2008-06-27 13:32:11 +1000 | [diff] [blame] | 1274 | if (dbp) |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1275 | xfs_trans_brelse(tp, dbp); |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame] | 1276 | error = xfs_dir3_data_read(tp, dp, |
Dave Chinner | 2998ab1 | 2014-06-06 15:07:53 +1000 | [diff] [blame] | 1277 | xfs_dir2_db_to_da(args->geo, newdb), |
| 1278 | -1, &dbp); |
Barry Naujok | 5163f95 | 2008-05-21 16:41:01 +1000 | [diff] [blame] | 1279 | if (error) { |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1280 | xfs_trans_brelse(tp, lbp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1281 | return error; |
| 1282 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1283 | curdb = newdb; |
| 1284 | } |
| 1285 | /* |
| 1286 | * Point to the data entry. |
| 1287 | */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1288 | dep = (xfs_dir2_data_entry_t *)((char *)dbp->b_addr + |
Dave Chinner | 3002803 | 2014-06-06 15:08:18 +1000 | [diff] [blame] | 1289 | xfs_dir2_dataptr_to_off(args->geo, |
| 1290 | be32_to_cpu(lep->address))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1291 | /* |
Barry Naujok | 5163f95 | 2008-05-21 16:41:01 +1000 | [diff] [blame] | 1292 | * Compare name and if it's an exact match, return the index |
| 1293 | * and buffer. If it's the first case-insensitive match, store |
| 1294 | * the index and buffer and continue looking for an exact match. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1295 | */ |
Barry Naujok | 5163f95 | 2008-05-21 16:41:01 +1000 | [diff] [blame] | 1296 | cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen); |
| 1297 | if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) { |
| 1298 | args->cmpresult = cmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1299 | *indexp = index; |
Barry Naujok | 07fe4dd | 2008-06-27 13:32:11 +1000 | [diff] [blame] | 1300 | /* case exact match: return the current buffer. */ |
Barry Naujok | 5163f95 | 2008-05-21 16:41:01 +1000 | [diff] [blame] | 1301 | if (cmp == XFS_CMP_EXACT) { |
Barry Naujok | 5163f95 | 2008-05-21 16:41:01 +1000 | [diff] [blame] | 1302 | *dbpp = dbp; |
| 1303 | return 0; |
| 1304 | } |
Barry Naujok | 07fe4dd | 2008-06-27 13:32:11 +1000 | [diff] [blame] | 1305 | cidb = curdb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1306 | } |
| 1307 | } |
Barry Naujok | 6a17810 | 2008-05-21 16:42:05 +1000 | [diff] [blame] | 1308 | ASSERT(args->op_flags & XFS_DA_OP_OKNOENT); |
Barry Naujok | 5163f95 | 2008-05-21 16:41:01 +1000 | [diff] [blame] | 1309 | /* |
Barry Naujok | 07fe4dd | 2008-06-27 13:32:11 +1000 | [diff] [blame] | 1310 | * Here, we can only be doing a lookup (not a rename or remove). |
| 1311 | * If a case-insensitive match was found earlier, re-read the |
| 1312 | * appropriate data block if required and return it. |
Barry Naujok | 5163f95 | 2008-05-21 16:41:01 +1000 | [diff] [blame] | 1313 | */ |
| 1314 | if (args->cmpresult == XFS_CMP_CASE) { |
Barry Naujok | 07fe4dd | 2008-06-27 13:32:11 +1000 | [diff] [blame] | 1315 | ASSERT(cidb != -1); |
| 1316 | if (cidb != curdb) { |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1317 | xfs_trans_brelse(tp, dbp); |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame] | 1318 | error = xfs_dir3_data_read(tp, dp, |
Dave Chinner | 2998ab1 | 2014-06-06 15:07:53 +1000 | [diff] [blame] | 1319 | xfs_dir2_db_to_da(args->geo, cidb), |
| 1320 | -1, &dbp); |
Barry Naujok | 07fe4dd | 2008-06-27 13:32:11 +1000 | [diff] [blame] | 1321 | if (error) { |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1322 | xfs_trans_brelse(tp, lbp); |
Barry Naujok | 07fe4dd | 2008-06-27 13:32:11 +1000 | [diff] [blame] | 1323 | return error; |
| 1324 | } |
| 1325 | } |
| 1326 | *dbpp = dbp; |
Barry Naujok | 5163f95 | 2008-05-21 16:41:01 +1000 | [diff] [blame] | 1327 | return 0; |
| 1328 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1329 | /* |
| 1330 | * No match found, return ENOENT. |
| 1331 | */ |
Barry Naujok | 07fe4dd | 2008-06-27 13:32:11 +1000 | [diff] [blame] | 1332 | ASSERT(cidb == -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1333 | if (dbp) |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1334 | xfs_trans_brelse(tp, dbp); |
| 1335 | xfs_trans_brelse(tp, lbp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1336 | return XFS_ERROR(ENOENT); |
| 1337 | } |
| 1338 | |
| 1339 | /* |
| 1340 | * Remove an entry from a leaf format directory. |
| 1341 | */ |
| 1342 | int /* error */ |
| 1343 | xfs_dir2_leaf_removename( |
| 1344 | xfs_da_args_t *args) /* operation arguments */ |
| 1345 | { |
Nathan Scott | 68b3a10 | 2006-03-17 17:27:19 +1100 | [diff] [blame] | 1346 | __be16 *bestsp; /* leaf block best freespace */ |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 1347 | xfs_dir2_data_hdr_t *hdr; /* data block header */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1348 | xfs_dir2_db_t db; /* data block number */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1349 | struct xfs_buf *dbp; /* data block buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1350 | xfs_dir2_data_entry_t *dep; /* data entry structure */ |
| 1351 | xfs_inode_t *dp; /* incore directory inode */ |
| 1352 | int error; /* error return code */ |
| 1353 | xfs_dir2_db_t i; /* temporary data block # */ |
| 1354 | int index; /* index into leaf entries */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1355 | struct xfs_buf *lbp; /* leaf buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1356 | xfs_dir2_leaf_t *leaf; /* leaf structure */ |
| 1357 | xfs_dir2_leaf_entry_t *lep; /* leaf entry */ |
| 1358 | xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */ |
| 1359 | xfs_mount_t *mp; /* filesystem mount point */ |
| 1360 | int needlog; /* need to log data header */ |
| 1361 | int needscan; /* need to rescan data frees */ |
| 1362 | xfs_dir2_data_off_t oldbest; /* old value of best free */ |
| 1363 | xfs_trans_t *tp; /* transaction pointer */ |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame] | 1364 | struct xfs_dir2_data_free *bf; /* bestfree table */ |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1365 | struct xfs_dir2_leaf_entry *ents; |
| 1366 | struct xfs_dir3_icleaf_hdr leafhdr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1367 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1368 | trace_xfs_dir2_leaf_removename(args); |
| 1369 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1370 | /* |
| 1371 | * Lookup the leaf entry, get the leaf and data blocks read in. |
| 1372 | */ |
| 1373 | if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) { |
| 1374 | return error; |
| 1375 | } |
| 1376 | dp = args->dp; |
| 1377 | tp = args->trans; |
| 1378 | mp = dp->i_mount; |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1379 | leaf = lbp->b_addr; |
| 1380 | hdr = dbp->b_addr; |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame] | 1381 | xfs_dir3_data_check(dp, dbp); |
Dave Chinner | 2ca9877 | 2013-10-29 22:11:49 +1100 | [diff] [blame] | 1382 | bf = dp->d_ops->data_bestfree_p(hdr); |
Dave Chinner | 01ba43b | 2013-10-29 22:11:52 +1100 | [diff] [blame] | 1383 | dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf); |
Dave Chinner | 4141956 | 2013-10-29 22:11:50 +1100 | [diff] [blame] | 1384 | ents = dp->d_ops->leaf_ents_p(leaf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1385 | /* |
| 1386 | * Point to the leaf entry, use that to point to the data entry. |
| 1387 | */ |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1388 | lep = &ents[index]; |
Dave Chinner | 3002803 | 2014-06-06 15:08:18 +1000 | [diff] [blame] | 1389 | db = xfs_dir2_dataptr_to_db(args->geo, be32_to_cpu(lep->address)); |
| 1390 | dep = (xfs_dir2_data_entry_t *)((char *)hdr + |
| 1391 | xfs_dir2_dataptr_to_off(args->geo, be32_to_cpu(lep->address))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1392 | needscan = needlog = 0; |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame] | 1393 | oldbest = be16_to_cpu(bf[0].length); |
Dave Chinner | 8f66193 | 2014-06-06 15:15:59 +1000 | [diff] [blame] | 1394 | ltp = xfs_dir2_leaf_tail_p(args->geo, leaf); |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1395 | bestsp = xfs_dir2_leaf_bests_p(ltp); |
Nathan Scott | 68b3a10 | 2006-03-17 17:27:19 +1100 | [diff] [blame] | 1396 | ASSERT(be16_to_cpu(bestsp[db]) == oldbest); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1397 | /* |
| 1398 | * Mark the former data entry unused. |
| 1399 | */ |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame^] | 1400 | xfs_dir2_data_make_free(args, dbp, |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 1401 | (xfs_dir2_data_aoff_t)((char *)dep - (char *)hdr), |
Dave Chinner | 9d23fc8 | 2013-10-29 22:11:48 +1100 | [diff] [blame] | 1402 | dp->d_ops->data_entsize(dep->namelen), &needlog, &needscan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1403 | /* |
| 1404 | * We just mark the leaf entry stale by putting a null in it. |
| 1405 | */ |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1406 | leafhdr.stale++; |
Dave Chinner | 01ba43b | 2013-10-29 22:11:52 +1100 | [diff] [blame] | 1407 | dp->d_ops->leaf_hdr_to_disk(leaf, &leafhdr); |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame^] | 1408 | xfs_dir3_leaf_log_header(args, lbp); |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1409 | |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 1410 | lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR); |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame^] | 1411 | xfs_dir3_leaf_log_ents(args, lbp, index, index); |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1412 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1413 | /* |
| 1414 | * Scan the freespace in the data block again if necessary, |
| 1415 | * log the data block header if necessary. |
| 1416 | */ |
| 1417 | if (needscan) |
Dave Chinner | 9d23fc8 | 2013-10-29 22:11:48 +1100 | [diff] [blame] | 1418 | xfs_dir2_data_freescan(dp, hdr, &needlog); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1419 | if (needlog) |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame^] | 1420 | xfs_dir2_data_log_header(args, dbp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1421 | /* |
| 1422 | * If the longest freespace in the data block has changed, |
| 1423 | * put the new value in the bests table and log that. |
| 1424 | */ |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame] | 1425 | if (be16_to_cpu(bf[0].length) != oldbest) { |
| 1426 | bestsp[db] = bf[0].length; |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame^] | 1427 | xfs_dir3_leaf_log_bests(args, lbp, db, db); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1428 | } |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame] | 1429 | xfs_dir3_data_check(dp, dbp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1430 | /* |
| 1431 | * If the data block is now empty then get rid of the data block. |
| 1432 | */ |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame] | 1433 | if (be16_to_cpu(bf[0].length) == |
Dave Chinner | 8f66193 | 2014-06-06 15:15:59 +1000 | [diff] [blame] | 1434 | args->geo->blksize - dp->d_ops->data_entry_offset) { |
Dave Chinner | 7dda6e8 | 2014-06-06 15:11:18 +1000 | [diff] [blame] | 1435 | ASSERT(db != args->geo->datablk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1436 | if ((error = xfs_dir2_shrink_inode(args, db, dbp))) { |
| 1437 | /* |
| 1438 | * Nope, can't get rid of it because it caused |
| 1439 | * allocation of a bmap btree block to do so. |
| 1440 | * Just go on, returning success, leaving the |
| 1441 | * empty block in place. |
| 1442 | */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1443 | if (error == ENOSPC && args->total == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1444 | error = 0; |
Dave Chinner | 4141956 | 2013-10-29 22:11:50 +1100 | [diff] [blame] | 1445 | xfs_dir3_leaf_check(dp, lbp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1446 | return error; |
| 1447 | } |
| 1448 | dbp = NULL; |
| 1449 | /* |
| 1450 | * If this is the last data block then compact the |
| 1451 | * bests table by getting rid of entries. |
| 1452 | */ |
Nathan Scott | afbcb3f | 2006-03-17 17:27:28 +1100 | [diff] [blame] | 1453 | if (db == be32_to_cpu(ltp->bestcount) - 1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1454 | /* |
| 1455 | * Look for the last active entry (i). |
| 1456 | */ |
| 1457 | for (i = db - 1; i > 0; i--) { |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 1458 | if (bestsp[i] != cpu_to_be16(NULLDATAOFF)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1459 | break; |
| 1460 | } |
| 1461 | /* |
| 1462 | * Copy the table down so inactive entries at the |
| 1463 | * end are removed. |
| 1464 | */ |
| 1465 | memmove(&bestsp[db - i], bestsp, |
Nathan Scott | afbcb3f | 2006-03-17 17:27:28 +1100 | [diff] [blame] | 1466 | (be32_to_cpu(ltp->bestcount) - (db - i)) * sizeof(*bestsp)); |
Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 1467 | be32_add_cpu(<p->bestcount, -(db - i)); |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame^] | 1468 | xfs_dir3_leaf_log_tail(args, lbp); |
| 1469 | xfs_dir3_leaf_log_bests(args, lbp, 0, |
| 1470 | be32_to_cpu(ltp->bestcount) - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1471 | } else |
Nathan Scott | 68b3a10 | 2006-03-17 17:27:19 +1100 | [diff] [blame] | 1472 | bestsp[db] = cpu_to_be16(NULLDATAOFF); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1473 | } |
| 1474 | /* |
| 1475 | * If the data block was not the first one, drop it. |
| 1476 | */ |
Dave Chinner | 7dda6e8 | 2014-06-06 15:11:18 +1000 | [diff] [blame] | 1477 | else if (db != args->geo->datablk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1478 | dbp = NULL; |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1479 | |
Dave Chinner | 4141956 | 2013-10-29 22:11:50 +1100 | [diff] [blame] | 1480 | xfs_dir3_leaf_check(dp, lbp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1481 | /* |
| 1482 | * See if we can convert to block form. |
| 1483 | */ |
| 1484 | return xfs_dir2_leaf_to_block(args, lbp, dbp); |
| 1485 | } |
| 1486 | |
| 1487 | /* |
| 1488 | * Replace the inode number in a leaf format directory entry. |
| 1489 | */ |
| 1490 | int /* error */ |
| 1491 | xfs_dir2_leaf_replace( |
| 1492 | xfs_da_args_t *args) /* operation arguments */ |
| 1493 | { |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1494 | struct xfs_buf *dbp; /* data block buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1495 | xfs_dir2_data_entry_t *dep; /* data block entry */ |
| 1496 | xfs_inode_t *dp; /* incore directory inode */ |
| 1497 | int error; /* error return code */ |
| 1498 | int index; /* index of leaf entry */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1499 | struct xfs_buf *lbp; /* leaf buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1500 | xfs_dir2_leaf_t *leaf; /* leaf structure */ |
| 1501 | xfs_dir2_leaf_entry_t *lep; /* leaf entry */ |
| 1502 | xfs_trans_t *tp; /* transaction pointer */ |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1503 | struct xfs_dir2_leaf_entry *ents; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1504 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1505 | trace_xfs_dir2_leaf_replace(args); |
| 1506 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1507 | /* |
| 1508 | * Look up the entry. |
| 1509 | */ |
| 1510 | if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) { |
| 1511 | return error; |
| 1512 | } |
| 1513 | dp = args->dp; |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1514 | leaf = lbp->b_addr; |
Dave Chinner | 4141956 | 2013-10-29 22:11:50 +1100 | [diff] [blame] | 1515 | ents = dp->d_ops->leaf_ents_p(leaf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1516 | /* |
| 1517 | * Point to the leaf entry, get data address from it. |
| 1518 | */ |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1519 | lep = &ents[index]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1520 | /* |
| 1521 | * Point to the data entry. |
| 1522 | */ |
| 1523 | dep = (xfs_dir2_data_entry_t *) |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1524 | ((char *)dbp->b_addr + |
Dave Chinner | 3002803 | 2014-06-06 15:08:18 +1000 | [diff] [blame] | 1525 | xfs_dir2_dataptr_to_off(args->geo, be32_to_cpu(lep->address))); |
Christoph Hellwig | ff9901c | 2006-06-09 14:48:37 +1000 | [diff] [blame] | 1526 | ASSERT(args->inumber != be64_to_cpu(dep->inumber)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1527 | /* |
| 1528 | * Put the new inode number in, log it. |
| 1529 | */ |
Christoph Hellwig | ff9901c | 2006-06-09 14:48:37 +1000 | [diff] [blame] | 1530 | dep->inumber = cpu_to_be64(args->inumber); |
Dave Chinner | 9d23fc8 | 2013-10-29 22:11:48 +1100 | [diff] [blame] | 1531 | dp->d_ops->data_put_ftype(dep, args->filetype); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1532 | tp = args->trans; |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame^] | 1533 | xfs_dir2_data_log_entry(args, dbp, dep); |
Dave Chinner | 4141956 | 2013-10-29 22:11:50 +1100 | [diff] [blame] | 1534 | xfs_dir3_leaf_check(dp, lbp); |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1535 | xfs_trans_brelse(tp, lbp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1536 | return 0; |
| 1537 | } |
| 1538 | |
| 1539 | /* |
| 1540 | * Return index in the leaf block (lbp) which is either the first |
| 1541 | * one with this hash value, or if there are none, the insert point |
| 1542 | * for that hash value. |
| 1543 | */ |
| 1544 | int /* index value */ |
| 1545 | xfs_dir2_leaf_search_hash( |
| 1546 | xfs_da_args_t *args, /* operation arguments */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1547 | struct xfs_buf *lbp) /* leaf buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1548 | { |
| 1549 | xfs_dahash_t hash=0; /* hash from this entry */ |
| 1550 | xfs_dahash_t hashwant; /* hash value looking for */ |
| 1551 | int high; /* high leaf index */ |
| 1552 | int low; /* low leaf index */ |
| 1553 | xfs_dir2_leaf_t *leaf; /* leaf structure */ |
| 1554 | xfs_dir2_leaf_entry_t *lep; /* leaf entry */ |
| 1555 | int mid=0; /* current leaf index */ |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1556 | struct xfs_dir2_leaf_entry *ents; |
| 1557 | struct xfs_dir3_icleaf_hdr leafhdr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1558 | |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1559 | leaf = lbp->b_addr; |
Dave Chinner | 4141956 | 2013-10-29 22:11:50 +1100 | [diff] [blame] | 1560 | ents = args->dp->d_ops->leaf_ents_p(leaf); |
Dave Chinner | 01ba43b | 2013-10-29 22:11:52 +1100 | [diff] [blame] | 1561 | args->dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf); |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1562 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1563 | /* |
| 1564 | * Note, the table cannot be empty, so we have to go through the loop. |
| 1565 | * Binary search the leaf entries looking for our hash value. |
| 1566 | */ |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1567 | for (lep = ents, low = 0, high = leafhdr.count - 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1568 | hashwant = args->hashval; |
| 1569 | low <= high; ) { |
| 1570 | mid = (low + high) >> 1; |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 1571 | if ((hash = be32_to_cpu(lep[mid].hashval)) == hashwant) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1572 | break; |
| 1573 | if (hash < hashwant) |
| 1574 | low = mid + 1; |
| 1575 | else |
| 1576 | high = mid - 1; |
| 1577 | } |
| 1578 | /* |
| 1579 | * Found one, back up through all the equal hash values. |
| 1580 | */ |
| 1581 | if (hash == hashwant) { |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 1582 | while (mid > 0 && be32_to_cpu(lep[mid - 1].hashval) == hashwant) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1583 | mid--; |
| 1584 | } |
| 1585 | } |
| 1586 | /* |
| 1587 | * Need to point to an entry higher than ours. |
| 1588 | */ |
| 1589 | else if (hash < hashwant) |
| 1590 | mid++; |
| 1591 | return mid; |
| 1592 | } |
| 1593 | |
| 1594 | /* |
| 1595 | * Trim off a trailing data block. We know it's empty since the leaf |
| 1596 | * freespace table says so. |
| 1597 | */ |
| 1598 | int /* error */ |
| 1599 | xfs_dir2_leaf_trim_data( |
| 1600 | xfs_da_args_t *args, /* operation arguments */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1601 | struct xfs_buf *lbp, /* leaf buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1602 | xfs_dir2_db_t db) /* data block number */ |
| 1603 | { |
Nathan Scott | 68b3a10 | 2006-03-17 17:27:19 +1100 | [diff] [blame] | 1604 | __be16 *bestsp; /* leaf bests table */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1605 | struct xfs_buf *dbp; /* data block buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1606 | xfs_inode_t *dp; /* incore directory inode */ |
| 1607 | int error; /* error return value */ |
| 1608 | xfs_dir2_leaf_t *leaf; /* leaf structure */ |
| 1609 | xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */ |
| 1610 | xfs_mount_t *mp; /* filesystem mount point */ |
| 1611 | xfs_trans_t *tp; /* transaction pointer */ |
| 1612 | |
| 1613 | dp = args->dp; |
| 1614 | mp = dp->i_mount; |
| 1615 | tp = args->trans; |
| 1616 | /* |
| 1617 | * Read the offending data block. We need its buffer. |
| 1618 | */ |
Dave Chinner | 2998ab1 | 2014-06-06 15:07:53 +1000 | [diff] [blame] | 1619 | error = xfs_dir3_data_read(tp, dp, xfs_dir2_db_to_da(args->geo, db), |
| 1620 | -1, &dbp); |
Dave Chinner | 4bb20a8 | 2012-11-12 22:54:10 +1100 | [diff] [blame] | 1621 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1622 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1623 | |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1624 | leaf = lbp->b_addr; |
Dave Chinner | 8f66193 | 2014-06-06 15:15:59 +1000 | [diff] [blame] | 1625 | ltp = xfs_dir2_leaf_tail_p(args->geo, leaf); |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 1626 | |
| 1627 | #ifdef DEBUG |
| 1628 | { |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1629 | struct xfs_dir2_data_hdr *hdr = dbp->b_addr; |
Dave Chinner | 2ca9877 | 2013-10-29 22:11:49 +1100 | [diff] [blame] | 1630 | struct xfs_dir2_data_free *bf = dp->d_ops->data_bestfree_p(hdr); |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 1631 | |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame] | 1632 | ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) || |
| 1633 | hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC)); |
| 1634 | ASSERT(be16_to_cpu(bf[0].length) == |
Dave Chinner | 8f66193 | 2014-06-06 15:15:59 +1000 | [diff] [blame] | 1635 | args->geo->blksize - dp->d_ops->data_entry_offset); |
Nathan Scott | afbcb3f | 2006-03-17 17:27:28 +1100 | [diff] [blame] | 1636 | ASSERT(db == be32_to_cpu(ltp->bestcount) - 1); |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 1637 | } |
| 1638 | #endif |
| 1639 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1640 | /* |
| 1641 | * Get rid of the data block. |
| 1642 | */ |
| 1643 | if ((error = xfs_dir2_shrink_inode(args, db, dbp))) { |
| 1644 | ASSERT(error != ENOSPC); |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1645 | xfs_trans_brelse(tp, dbp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1646 | return error; |
| 1647 | } |
| 1648 | /* |
| 1649 | * Eliminate the last bests entry from the table. |
| 1650 | */ |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1651 | bestsp = xfs_dir2_leaf_bests_p(ltp); |
Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 1652 | be32_add_cpu(<p->bestcount, -1); |
Nathan Scott | afbcb3f | 2006-03-17 17:27:28 +1100 | [diff] [blame] | 1653 | memmove(&bestsp[1], &bestsp[0], be32_to_cpu(ltp->bestcount) * sizeof(*bestsp)); |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame^] | 1654 | xfs_dir3_leaf_log_tail(args, lbp); |
| 1655 | xfs_dir3_leaf_log_bests(args, lbp, 0, be32_to_cpu(ltp->bestcount) - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1656 | return 0; |
| 1657 | } |
| 1658 | |
Christoph Hellwig | 2282396 | 2011-07-08 14:35:53 +0200 | [diff] [blame] | 1659 | static inline size_t |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1660 | xfs_dir3_leaf_size( |
| 1661 | struct xfs_dir3_icleaf_hdr *hdr, |
Christoph Hellwig | 2282396 | 2011-07-08 14:35:53 +0200 | [diff] [blame] | 1662 | int counts) |
| 1663 | { |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1664 | int entries; |
| 1665 | int hdrsize; |
Christoph Hellwig | 2282396 | 2011-07-08 14:35:53 +0200 | [diff] [blame] | 1666 | |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1667 | entries = hdr->count - hdr->stale; |
| 1668 | if (hdr->magic == XFS_DIR2_LEAF1_MAGIC || |
| 1669 | hdr->magic == XFS_DIR2_LEAFN_MAGIC) |
| 1670 | hdrsize = sizeof(struct xfs_dir2_leaf_hdr); |
| 1671 | else |
| 1672 | hdrsize = sizeof(struct xfs_dir3_leaf_hdr); |
| 1673 | |
| 1674 | return hdrsize + entries * sizeof(xfs_dir2_leaf_entry_t) |
| 1675 | + counts * sizeof(xfs_dir2_data_off_t) |
| 1676 | + sizeof(xfs_dir2_leaf_tail_t); |
Christoph Hellwig | 2282396 | 2011-07-08 14:35:53 +0200 | [diff] [blame] | 1677 | } |
| 1678 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1679 | /* |
| 1680 | * Convert node form directory to leaf form directory. |
| 1681 | * The root of the node form dir needs to already be a LEAFN block. |
| 1682 | * Just return if we can't do anything. |
| 1683 | */ |
| 1684 | int /* error */ |
| 1685 | xfs_dir2_node_to_leaf( |
| 1686 | xfs_da_state_t *state) /* directory operation state */ |
| 1687 | { |
| 1688 | xfs_da_args_t *args; /* operation arguments */ |
| 1689 | xfs_inode_t *dp; /* incore directory inode */ |
| 1690 | int error; /* error return code */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1691 | struct xfs_buf *fbp; /* buffer for freespace block */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1692 | xfs_fileoff_t fo; /* freespace file offset */ |
| 1693 | xfs_dir2_free_t *free; /* freespace structure */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1694 | struct xfs_buf *lbp; /* buffer for leaf block */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1695 | xfs_dir2_leaf_tail_t *ltp; /* tail of leaf structure */ |
| 1696 | xfs_dir2_leaf_t *leaf; /* leaf structure */ |
| 1697 | xfs_mount_t *mp; /* filesystem mount point */ |
| 1698 | int rval; /* successful free trim? */ |
| 1699 | xfs_trans_t *tp; /* transaction pointer */ |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1700 | struct xfs_dir3_icleaf_hdr leafhdr; |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 1701 | struct xfs_dir3_icfree_hdr freehdr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1702 | |
| 1703 | /* |
| 1704 | * There's more than a leaf level in the btree, so there must |
| 1705 | * be multiple leafn blocks. Give up. |
| 1706 | */ |
| 1707 | if (state->path.active > 1) |
| 1708 | return 0; |
| 1709 | args = state->args; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1710 | |
| 1711 | trace_xfs_dir2_node_to_leaf(args); |
| 1712 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1713 | mp = state->mp; |
| 1714 | dp = args->dp; |
| 1715 | tp = args->trans; |
| 1716 | /* |
| 1717 | * Get the last offset in the file. |
| 1718 | */ |
Eric Sandeen | 7fb2cd4 | 2014-04-14 18:58:05 +1000 | [diff] [blame] | 1719 | if ((error = xfs_bmap_last_offset(dp, &fo, XFS_DATA_FORK))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1720 | return error; |
| 1721 | } |
Dave Chinner | d6cf130 | 2014-06-06 15:14:11 +1000 | [diff] [blame] | 1722 | fo -= args->geo->fsbcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1723 | /* |
| 1724 | * If there are freespace blocks other than the first one, |
| 1725 | * take this opportunity to remove trailing empty freespace blocks |
| 1726 | * that may have been left behind during no-space-reservation |
| 1727 | * operations. |
| 1728 | */ |
Dave Chinner | 7dda6e8 | 2014-06-06 15:11:18 +1000 | [diff] [blame] | 1729 | while (fo > args->geo->freeblk) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1730 | if ((error = xfs_dir2_node_trim_free(args, fo, &rval))) { |
| 1731 | return error; |
| 1732 | } |
| 1733 | if (rval) |
Dave Chinner | d6cf130 | 2014-06-06 15:14:11 +1000 | [diff] [blame] | 1734 | fo -= args->geo->fsbcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1735 | else |
| 1736 | return 0; |
| 1737 | } |
| 1738 | /* |
| 1739 | * Now find the block just before the freespace block. |
| 1740 | */ |
| 1741 | if ((error = xfs_bmap_last_before(tp, dp, &fo, XFS_DATA_FORK))) { |
| 1742 | return error; |
| 1743 | } |
| 1744 | /* |
| 1745 | * If it's not the single leaf block, give up. |
| 1746 | */ |
Dave Chinner | 8f66193 | 2014-06-06 15:15:59 +1000 | [diff] [blame] | 1747 | if (XFS_FSB_TO_B(mp, fo) > XFS_DIR2_LEAF_OFFSET + args->geo->blksize) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1748 | return 0; |
| 1749 | lbp = state->path.blk[0].bp; |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1750 | leaf = lbp->b_addr; |
Dave Chinner | 01ba43b | 2013-10-29 22:11:52 +1100 | [diff] [blame] | 1751 | dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf); |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1752 | |
| 1753 | ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC || |
| 1754 | leafhdr.magic == XFS_DIR3_LEAFN_MAGIC); |
| 1755 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1756 | /* |
| 1757 | * Read the freespace block. |
| 1758 | */ |
Dave Chinner | 7dda6e8 | 2014-06-06 15:11:18 +1000 | [diff] [blame] | 1759 | error = xfs_dir2_free_read(tp, dp, args->geo->freeblk, &fbp); |
Dave Chinner | 4bb20a8 | 2012-11-12 22:54:10 +1100 | [diff] [blame] | 1760 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1761 | return error; |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1762 | free = fbp->b_addr; |
Dave Chinner | 01ba43b | 2013-10-29 22:11:52 +1100 | [diff] [blame] | 1763 | dp->d_ops->free_hdr_from_disk(&freehdr, free); |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 1764 | |
| 1765 | ASSERT(!freehdr.firstdb); |
Christoph Hellwig | 2282396 | 2011-07-08 14:35:53 +0200 | [diff] [blame] | 1766 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1767 | /* |
| 1768 | * Now see if the leafn and free data will fit in a leaf1. |
| 1769 | * If not, release the buffer and give up. |
| 1770 | */ |
Dave Chinner | 8f66193 | 2014-06-06 15:15:59 +1000 | [diff] [blame] | 1771 | if (xfs_dir3_leaf_size(&leafhdr, freehdr.nvalid) > args->geo->blksize) { |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1772 | xfs_trans_brelse(tp, fbp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1773 | return 0; |
| 1774 | } |
Christoph Hellwig | 2282396 | 2011-07-08 14:35:53 +0200 | [diff] [blame] | 1775 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1776 | /* |
| 1777 | * If the leaf has any stale entries in it, compress them out. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1778 | */ |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1779 | if (leafhdr.stale) |
| 1780 | xfs_dir3_leaf_compact(args, &leafhdr, lbp); |
Dave Chinner | b0f539d | 2012-11-14 17:53:49 +1100 | [diff] [blame] | 1781 | |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1782 | lbp->b_ops = &xfs_dir3_leaf1_buf_ops; |
Dave Chinner | 61fe135 | 2013-04-03 16:11:30 +1100 | [diff] [blame] | 1783 | xfs_trans_buf_set_type(tp, lbp, XFS_BLFT_DIR_LEAF1_BUF); |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1784 | leafhdr.magic = (leafhdr.magic == XFS_DIR2_LEAFN_MAGIC) |
| 1785 | ? XFS_DIR2_LEAF1_MAGIC |
| 1786 | : XFS_DIR3_LEAF1_MAGIC; |
Dave Chinner | b0f539d | 2012-11-14 17:53:49 +1100 | [diff] [blame] | 1787 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1788 | /* |
| 1789 | * Set up the leaf tail from the freespace block. |
| 1790 | */ |
Dave Chinner | 8f66193 | 2014-06-06 15:15:59 +1000 | [diff] [blame] | 1791 | ltp = xfs_dir2_leaf_tail_p(args->geo, leaf); |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 1792 | ltp->bestcount = cpu_to_be32(freehdr.nvalid); |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1793 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1794 | /* |
| 1795 | * Set up the leaf bests table. |
| 1796 | */ |
Dave Chinner | 24dd0f5 | 2013-10-30 13:48:41 -0500 | [diff] [blame] | 1797 | memcpy(xfs_dir2_leaf_bests_p(ltp), dp->d_ops->free_bests_p(free), |
Dave Chinner | cbc8adf | 2013-04-03 16:11:21 +1100 | [diff] [blame] | 1798 | freehdr.nvalid * sizeof(xfs_dir2_data_off_t)); |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1799 | |
Dave Chinner | 01ba43b | 2013-10-29 22:11:52 +1100 | [diff] [blame] | 1800 | dp->d_ops->leaf_hdr_to_disk(leaf, &leafhdr); |
Dave Chinner | bc85178 | 2014-06-06 15:20:54 +1000 | [diff] [blame^] | 1801 | xfs_dir3_leaf_log_header(args, lbp); |
| 1802 | xfs_dir3_leaf_log_bests(args, lbp, 0, be32_to_cpu(ltp->bestcount) - 1); |
| 1803 | xfs_dir3_leaf_log_tail(args, lbp); |
Dave Chinner | 4141956 | 2013-10-29 22:11:50 +1100 | [diff] [blame] | 1804 | xfs_dir3_leaf_check(dp, lbp); |
Dave Chinner | 24df33b | 2013-04-12 07:30:21 +1000 | [diff] [blame] | 1805 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1806 | /* |
| 1807 | * Get rid of the freespace block. |
| 1808 | */ |
Dave Chinner | 8c44a28 | 2014-06-06 15:04:41 +1000 | [diff] [blame] | 1809 | error = xfs_dir2_shrink_inode(args, |
Dave Chinner | 3002803 | 2014-06-06 15:08:18 +1000 | [diff] [blame] | 1810 | xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET), |
| 1811 | fbp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1812 | if (error) { |
| 1813 | /* |
| 1814 | * This can't fail here because it can only happen when |
| 1815 | * punching out the middle of an extent, and this is an |
| 1816 | * isolated block. |
| 1817 | */ |
| 1818 | ASSERT(error != ENOSPC); |
| 1819 | return error; |
| 1820 | } |
| 1821 | fbp = NULL; |
| 1822 | /* |
| 1823 | * Now see if we can convert the single-leaf directory |
| 1824 | * down to a block form directory. |
| 1825 | * This routine always kills the dabuf for the leaf, so |
| 1826 | * eliminate it from the path. |
| 1827 | */ |
| 1828 | error = xfs_dir2_leaf_to_block(args, lbp, NULL); |
| 1829 | state->path.blk[0].bp = NULL; |
| 1830 | return error; |
| 1831 | } |