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