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