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