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