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 | f5f3d9b | 2013-04-03 16:11:20 +1100 | [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" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include "xfs_log.h" |
| 23 | #include "xfs_trans.h" |
| 24 | #include "xfs_sb.h" |
David Chinner | da353b0 | 2007-08-28 14:00:13 +1000 | [diff] [blame] | 25 | #include "xfs_ag.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include "xfs_mount.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 27 | #include "xfs_da_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include "xfs_bmap_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include "xfs_dinode.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include "xfs_inode.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 31 | #include "xfs_inode_item.h" |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 32 | #include "xfs_buf_item.h" |
Dave Chinner | 8d2a5e6 | 2012-03-07 04:50:19 +0000 | [diff] [blame] | 33 | #include "xfs_dir2.h" |
Christoph Hellwig | 5792664 | 2011-07-13 13:43:48 +0200 | [diff] [blame] | 34 | #include "xfs_dir2_format.h" |
| 35 | #include "xfs_dir2_priv.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include "xfs_error.h" |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 37 | #include "xfs_trace.h" |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 38 | #include "xfs_cksum.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
| 40 | /* |
| 41 | * Local function prototypes. |
| 42 | */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 43 | static void xfs_dir2_block_log_leaf(xfs_trans_t *tp, struct xfs_buf *bp, |
| 44 | int first, int last); |
| 45 | static void xfs_dir2_block_log_tail(xfs_trans_t *tp, struct xfs_buf *bp); |
| 46 | static int xfs_dir2_block_lookup_int(xfs_da_args_t *args, struct xfs_buf **bpp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | int *entno); |
| 48 | static int xfs_dir2_block_sort(const void *a, const void *b); |
| 49 | |
Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 50 | static xfs_dahash_t xfs_dir_hash_dot, xfs_dir_hash_dotdot; |
| 51 | |
| 52 | /* |
| 53 | * One-time startup routine called from xfs_init(). |
| 54 | */ |
| 55 | void |
| 56 | xfs_dir_startup(void) |
| 57 | { |
Dave Chinner | 4a24cb7 | 2010-01-20 10:48:05 +1100 | [diff] [blame] | 58 | xfs_dir_hash_dot = xfs_da_hashname((unsigned char *)".", 1); |
| 59 | xfs_dir_hash_dotdot = xfs_da_hashname((unsigned char *)"..", 2); |
Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 60 | } |
| 61 | |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 62 | static bool |
| 63 | xfs_dir3_block_verify( |
Dave Chinner | 82025d7 | 2012-11-12 22:54:12 +1100 | [diff] [blame] | 64 | struct xfs_buf *bp) |
| 65 | { |
| 66 | struct xfs_mount *mp = bp->b_target->bt_mount; |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 67 | struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr; |
Dave Chinner | 82025d7 | 2012-11-12 22:54:12 +1100 | [diff] [blame] | 68 | |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 69 | if (xfs_sb_version_hascrc(&mp->m_sb)) { |
| 70 | if (hdr3->magic != cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) |
| 71 | return false; |
| 72 | if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_uuid)) |
| 73 | return false; |
| 74 | if (be64_to_cpu(hdr3->blkno) != bp->b_bn) |
| 75 | return false; |
| 76 | } else { |
| 77 | if (hdr3->magic != cpu_to_be32(XFS_DIR2_BLOCK_MAGIC)) |
| 78 | return false; |
| 79 | } |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame^] | 80 | if (__xfs_dir3_data_check(NULL, bp)) |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 81 | return false; |
| 82 | return true; |
| 83 | } |
Dave Chinner | 82025d7 | 2012-11-12 22:54:12 +1100 | [diff] [blame] | 84 | |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 85 | static void |
| 86 | xfs_dir3_block_read_verify( |
| 87 | struct xfs_buf *bp) |
| 88 | { |
| 89 | struct xfs_mount *mp = bp->b_target->bt_mount; |
| 90 | |
| 91 | if ((xfs_sb_version_hascrc(&mp->m_sb) && |
| 92 | !xfs_verify_cksum(bp->b_addr, BBTOB(bp->b_length), |
| 93 | XFS_DIR3_DATA_CRC_OFF)) || |
| 94 | !xfs_dir3_block_verify(bp)) { |
| 95 | XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr); |
Dave Chinner | 82025d7 | 2012-11-12 22:54:12 +1100 | [diff] [blame] | 96 | xfs_buf_ioerror(bp, EFSCORRUPTED); |
| 97 | } |
Dave Chinner | 612cfbf | 2012-11-14 17:52:32 +1100 | [diff] [blame] | 98 | } |
Dave Chinner | 82025d7 | 2012-11-12 22:54:12 +1100 | [diff] [blame] | 99 | |
Dave Chinner | 612cfbf | 2012-11-14 17:52:32 +1100 | [diff] [blame] | 100 | static void |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 101 | xfs_dir3_block_write_verify( |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 102 | struct xfs_buf *bp) |
| 103 | { |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 104 | struct xfs_mount *mp = bp->b_target->bt_mount; |
| 105 | struct xfs_buf_log_item *bip = bp->b_fspriv; |
| 106 | struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr; |
| 107 | |
| 108 | if (!xfs_dir3_block_verify(bp)) { |
| 109 | XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr); |
| 110 | xfs_buf_ioerror(bp, EFSCORRUPTED); |
| 111 | return; |
| 112 | } |
| 113 | |
| 114 | if (!xfs_sb_version_hascrc(&mp->m_sb)) |
| 115 | return; |
| 116 | |
| 117 | if (bip) |
| 118 | hdr3->lsn = cpu_to_be64(bip->bli_item.li_lsn); |
| 119 | |
| 120 | xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length), XFS_DIR3_DATA_CRC_OFF); |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 121 | } |
| 122 | |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 123 | const struct xfs_buf_ops xfs_dir3_block_buf_ops = { |
| 124 | .verify_read = xfs_dir3_block_read_verify, |
| 125 | .verify_write = xfs_dir3_block_write_verify, |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 126 | }; |
Dave Chinner | 82025d7 | 2012-11-12 22:54:12 +1100 | [diff] [blame] | 127 | |
Dave Chinner | 20f7e9f | 2012-11-12 22:54:11 +1100 | [diff] [blame] | 128 | static int |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 129 | xfs_dir3_block_read( |
Dave Chinner | 20f7e9f | 2012-11-12 22:54:11 +1100 | [diff] [blame] | 130 | struct xfs_trans *tp, |
| 131 | struct xfs_inode *dp, |
| 132 | struct xfs_buf **bpp) |
| 133 | { |
| 134 | struct xfs_mount *mp = dp->i_mount; |
| 135 | |
| 136 | return xfs_da_read_buf(tp, dp, mp->m_dirdatablk, -1, bpp, |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 137 | XFS_DATA_FORK, &xfs_dir3_block_buf_ops); |
| 138 | } |
| 139 | |
| 140 | static void |
| 141 | xfs_dir3_block_init( |
| 142 | struct xfs_mount *mp, |
| 143 | struct xfs_buf *bp, |
| 144 | struct xfs_inode *dp) |
| 145 | { |
| 146 | struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr; |
| 147 | |
| 148 | bp->b_ops = &xfs_dir3_block_buf_ops; |
| 149 | |
| 150 | if (xfs_sb_version_hascrc(&mp->m_sb)) { |
| 151 | memset(hdr3, 0, sizeof(*hdr3)); |
| 152 | hdr3->magic = cpu_to_be32(XFS_DIR3_BLOCK_MAGIC); |
| 153 | hdr3->blkno = cpu_to_be64(bp->b_bn); |
| 154 | hdr3->owner = cpu_to_be64(dp->i_ino); |
| 155 | uuid_copy(&hdr3->uuid, &mp->m_sb.sb_uuid); |
| 156 | return; |
| 157 | |
| 158 | } |
| 159 | hdr3->magic = cpu_to_be32(XFS_DIR2_BLOCK_MAGIC); |
Dave Chinner | 20f7e9f | 2012-11-12 22:54:11 +1100 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | static void |
| 163 | xfs_dir2_block_need_space( |
| 164 | struct xfs_dir2_data_hdr *hdr, |
| 165 | struct xfs_dir2_block_tail *btp, |
| 166 | struct xfs_dir2_leaf_entry *blp, |
| 167 | __be16 **tagpp, |
| 168 | struct xfs_dir2_data_unused **dupp, |
| 169 | struct xfs_dir2_data_unused **enddupp, |
| 170 | int *compact, |
| 171 | int len) |
| 172 | { |
| 173 | struct xfs_dir2_data_free *bf; |
| 174 | __be16 *tagp = NULL; |
| 175 | struct xfs_dir2_data_unused *dup = NULL; |
| 176 | struct xfs_dir2_data_unused *enddup = NULL; |
| 177 | |
| 178 | *compact = 0; |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 179 | bf = xfs_dir3_data_bestfree_p(hdr); |
Dave Chinner | 20f7e9f | 2012-11-12 22:54:11 +1100 | [diff] [blame] | 180 | |
| 181 | /* |
| 182 | * If there are stale entries we'll use one for the leaf. |
| 183 | */ |
| 184 | if (btp->stale) { |
| 185 | if (be16_to_cpu(bf[0].length) >= len) { |
| 186 | /* |
| 187 | * The biggest entry enough to avoid compaction. |
| 188 | */ |
| 189 | dup = (xfs_dir2_data_unused_t *) |
| 190 | ((char *)hdr + be16_to_cpu(bf[0].offset)); |
| 191 | goto out; |
| 192 | } |
| 193 | |
| 194 | /* |
| 195 | * Will need to compact to make this work. |
| 196 | * Tag just before the first leaf entry. |
| 197 | */ |
| 198 | *compact = 1; |
| 199 | tagp = (__be16 *)blp - 1; |
| 200 | |
| 201 | /* Data object just before the first leaf entry. */ |
| 202 | dup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp)); |
| 203 | |
| 204 | /* |
| 205 | * If it's not free then the data will go where the |
| 206 | * leaf data starts now, if it works at all. |
| 207 | */ |
| 208 | if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) { |
| 209 | if (be16_to_cpu(dup->length) + (be32_to_cpu(btp->stale) - 1) * |
| 210 | (uint)sizeof(*blp) < len) |
| 211 | dup = NULL; |
| 212 | } else if ((be32_to_cpu(btp->stale) - 1) * (uint)sizeof(*blp) < len) |
| 213 | dup = NULL; |
| 214 | else |
| 215 | dup = (xfs_dir2_data_unused_t *)blp; |
| 216 | goto out; |
| 217 | } |
| 218 | |
| 219 | /* |
| 220 | * no stale entries, so just use free space. |
| 221 | * Tag just before the first leaf entry. |
| 222 | */ |
| 223 | tagp = (__be16 *)blp - 1; |
| 224 | |
| 225 | /* Data object just before the first leaf entry. */ |
| 226 | enddup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp)); |
| 227 | |
| 228 | /* |
| 229 | * If it's not free then can't do this add without cleaning up: |
| 230 | * the space before the first leaf entry needs to be free so it |
| 231 | * can be expanded to hold the pointer to the new entry. |
| 232 | */ |
| 233 | if (be16_to_cpu(enddup->freetag) == XFS_DIR2_DATA_FREE_TAG) { |
| 234 | /* |
| 235 | * Check out the biggest freespace and see if it's the same one. |
| 236 | */ |
| 237 | dup = (xfs_dir2_data_unused_t *) |
| 238 | ((char *)hdr + be16_to_cpu(bf[0].offset)); |
| 239 | if (dup != enddup) { |
| 240 | /* |
| 241 | * Not the same free entry, just check its length. |
| 242 | */ |
| 243 | if (be16_to_cpu(dup->length) < len) |
| 244 | dup = NULL; |
| 245 | goto out; |
| 246 | } |
| 247 | |
| 248 | /* |
| 249 | * It is the biggest freespace, can it hold the leaf too? |
| 250 | */ |
| 251 | if (be16_to_cpu(dup->length) < len + (uint)sizeof(*blp)) { |
| 252 | /* |
| 253 | * Yes, use the second-largest entry instead if it works. |
| 254 | */ |
| 255 | if (be16_to_cpu(bf[1].length) >= len) |
| 256 | dup = (xfs_dir2_data_unused_t *) |
| 257 | ((char *)hdr + be16_to_cpu(bf[1].offset)); |
| 258 | else |
| 259 | dup = NULL; |
| 260 | } |
| 261 | } |
| 262 | out: |
| 263 | *tagpp = tagp; |
| 264 | *dupp = dup; |
| 265 | *enddupp = enddup; |
| 266 | } |
| 267 | |
| 268 | /* |
| 269 | * compact the leaf entries. |
| 270 | * Leave the highest-numbered stale entry stale. |
| 271 | * XXX should be the one closest to mid but mid is not yet computed. |
| 272 | */ |
| 273 | static void |
| 274 | xfs_dir2_block_compact( |
| 275 | struct xfs_trans *tp, |
| 276 | struct xfs_buf *bp, |
| 277 | struct xfs_dir2_data_hdr *hdr, |
| 278 | struct xfs_dir2_block_tail *btp, |
| 279 | struct xfs_dir2_leaf_entry *blp, |
| 280 | int *needlog, |
| 281 | int *lfloghigh, |
| 282 | int *lfloglow) |
| 283 | { |
| 284 | int fromidx; /* source leaf index */ |
| 285 | int toidx; /* target leaf index */ |
| 286 | int needscan = 0; |
| 287 | int highstale; /* high stale index */ |
| 288 | |
| 289 | fromidx = toidx = be32_to_cpu(btp->count) - 1; |
| 290 | highstale = *lfloghigh = -1; |
| 291 | for (; fromidx >= 0; fromidx--) { |
| 292 | if (blp[fromidx].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) { |
| 293 | if (highstale == -1) |
| 294 | highstale = toidx; |
| 295 | else { |
| 296 | if (*lfloghigh == -1) |
| 297 | *lfloghigh = toidx; |
| 298 | continue; |
| 299 | } |
| 300 | } |
| 301 | if (fromidx < toidx) |
| 302 | blp[toidx] = blp[fromidx]; |
| 303 | toidx--; |
| 304 | } |
| 305 | *lfloglow = toidx + 1 - (be32_to_cpu(btp->stale) - 1); |
| 306 | *lfloghigh -= be32_to_cpu(btp->stale) - 1; |
| 307 | be32_add_cpu(&btp->count, -(be32_to_cpu(btp->stale) - 1)); |
| 308 | xfs_dir2_data_make_free(tp, bp, |
| 309 | (xfs_dir2_data_aoff_t)((char *)blp - (char *)hdr), |
| 310 | (xfs_dir2_data_aoff_t)((be32_to_cpu(btp->stale) - 1) * sizeof(*blp)), |
| 311 | needlog, &needscan); |
| 312 | blp += be32_to_cpu(btp->stale) - 1; |
| 313 | btp->stale = cpu_to_be32(1); |
| 314 | /* |
| 315 | * If we now need to rebuild the bestfree map, do so. |
| 316 | * This needs to happen before the next call to use_free. |
| 317 | */ |
| 318 | if (needscan) |
| 319 | xfs_dir2_data_freescan(tp->t_mountp, hdr, needlog); |
| 320 | } |
| 321 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | /* |
| 323 | * Add an entry to a block directory. |
| 324 | */ |
| 325 | int /* error */ |
| 326 | xfs_dir2_block_addname( |
| 327 | xfs_da_args_t *args) /* directory op arguments */ |
| 328 | { |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 329 | xfs_dir2_data_hdr_t *hdr; /* block header */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | xfs_dir2_leaf_entry_t *blp; /* block leaf entries */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 331 | struct xfs_buf *bp; /* buffer for block */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | xfs_dir2_block_tail_t *btp; /* block tail */ |
| 333 | int compact; /* need to compact leaf ents */ |
| 334 | xfs_dir2_data_entry_t *dep; /* block data entry */ |
| 335 | xfs_inode_t *dp; /* directory inode */ |
| 336 | xfs_dir2_data_unused_t *dup; /* block unused entry */ |
| 337 | int error; /* error return value */ |
| 338 | xfs_dir2_data_unused_t *enddup=NULL; /* unused at end of data */ |
| 339 | xfs_dahash_t hash; /* hash value of found entry */ |
| 340 | int high; /* high index for binary srch */ |
| 341 | int highstale; /* high stale index */ |
| 342 | int lfloghigh=0; /* last final leaf to log */ |
| 343 | int lfloglow=0; /* first final leaf to log */ |
| 344 | int len; /* length of the new entry */ |
| 345 | int low; /* low index for binary srch */ |
| 346 | int lowstale; /* low stale index */ |
| 347 | int mid=0; /* midpoint for binary srch */ |
| 348 | xfs_mount_t *mp; /* filesystem mount point */ |
| 349 | int needlog; /* need to log header */ |
| 350 | int needscan; /* need to rescan freespace */ |
Nathan Scott | 3d693c6 | 2006-03-17 17:28:27 +1100 | [diff] [blame] | 351 | __be16 *tagp; /* pointer to tag value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | xfs_trans_t *tp; /* transaction structure */ |
| 353 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 354 | trace_xfs_dir2_block_addname(args); |
| 355 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | dp = args->dp; |
| 357 | tp = args->trans; |
| 358 | mp = dp->i_mount; |
Dave Chinner | 20f7e9f | 2012-11-12 22:54:11 +1100 | [diff] [blame] | 359 | |
| 360 | /* Read the (one and only) directory block into bp. */ |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 361 | error = xfs_dir3_block_read(tp, dp, &bp); |
Dave Chinner | 4bb20a8 | 2012-11-12 22:54:10 +1100 | [diff] [blame] | 362 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | return error; |
Dave Chinner | 20f7e9f | 2012-11-12 22:54:11 +1100 | [diff] [blame] | 364 | |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 365 | len = xfs_dir2_data_entsize(args->namelen); |
Dave Chinner | 20f7e9f | 2012-11-12 22:54:11 +1100 | [diff] [blame] | 366 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | /* |
| 368 | * Set up pointers to parts of the block. |
| 369 | */ |
Dave Chinner | 20f7e9f | 2012-11-12 22:54:11 +1100 | [diff] [blame] | 370 | hdr = bp->b_addr; |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 371 | btp = xfs_dir2_block_tail_p(mp, hdr); |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 372 | blp = xfs_dir2_block_leaf_p(btp); |
Dave Chinner | 20f7e9f | 2012-11-12 22:54:11 +1100 | [diff] [blame] | 373 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | /* |
Dave Chinner | 20f7e9f | 2012-11-12 22:54:11 +1100 | [diff] [blame] | 375 | * Find out if we can reuse stale entries or whether we need extra |
| 376 | * space for entry and new leaf. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | */ |
Dave Chinner | 20f7e9f | 2012-11-12 22:54:11 +1100 | [diff] [blame] | 378 | xfs_dir2_block_need_space(hdr, btp, blp, &tagp, &dup, |
| 379 | &enddup, &compact, len); |
| 380 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | /* |
Dave Chinner | 20f7e9f | 2012-11-12 22:54:11 +1100 | [diff] [blame] | 382 | * Done everything we need for a space check now. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | */ |
Dave Chinner | 20f7e9f | 2012-11-12 22:54:11 +1100 | [diff] [blame] | 384 | if (args->op_flags & XFS_DA_OP_JUSTCHECK) { |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 385 | xfs_trans_brelse(tp, bp); |
Dave Chinner | 20f7e9f | 2012-11-12 22:54:11 +1100 | [diff] [blame] | 386 | if (!dup) |
| 387 | return XFS_ERROR(ENOSPC); |
| 388 | return 0; |
| 389 | } |
| 390 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | /* |
| 392 | * If we don't have space for the new entry & leaf ... |
| 393 | */ |
| 394 | if (!dup) { |
Dave Chinner | 20f7e9f | 2012-11-12 22:54:11 +1100 | [diff] [blame] | 395 | /* Don't have a space reservation: return no-space. */ |
| 396 | if (args->total == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | return XFS_ERROR(ENOSPC); |
| 398 | /* |
| 399 | * Convert to the next larger format. |
| 400 | * Then add the new entry in that format. |
| 401 | */ |
| 402 | error = xfs_dir2_block_to_leaf(args, bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | if (error) |
| 404 | return error; |
| 405 | return xfs_dir2_leaf_addname(args); |
| 406 | } |
Dave Chinner | 20f7e9f | 2012-11-12 22:54:11 +1100 | [diff] [blame] | 407 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | needlog = needscan = 0; |
Dave Chinner | 20f7e9f | 2012-11-12 22:54:11 +1100 | [diff] [blame] | 409 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | /* |
| 411 | * If need to compact the leaf entries, do it now. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | */ |
Eric Sandeen | 37f1356 | 2013-01-10 10:41:48 -0600 | [diff] [blame] | 413 | if (compact) { |
Dave Chinner | 20f7e9f | 2012-11-12 22:54:11 +1100 | [diff] [blame] | 414 | xfs_dir2_block_compact(tp, bp, hdr, btp, blp, &needlog, |
| 415 | &lfloghigh, &lfloglow); |
Eric Sandeen | 37f1356 | 2013-01-10 10:41:48 -0600 | [diff] [blame] | 416 | /* recalculate blp post-compaction */ |
| 417 | blp = xfs_dir2_block_leaf_p(btp); |
| 418 | } else if (btp->stale) { |
Dave Chinner | 20f7e9f | 2012-11-12 22:54:11 +1100 | [diff] [blame] | 419 | /* |
| 420 | * Set leaf logging boundaries to impossible state. |
| 421 | * For the no-stale case they're set explicitly. |
| 422 | */ |
Nathan Scott | e922fff | 2006-03-17 17:27:56 +1100 | [diff] [blame] | 423 | lfloglow = be32_to_cpu(btp->count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | lfloghigh = -1; |
| 425 | } |
Dave Chinner | 20f7e9f | 2012-11-12 22:54:11 +1100 | [diff] [blame] | 426 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | /* |
| 428 | * Find the slot that's first lower than our hash value, -1 if none. |
| 429 | */ |
Nathan Scott | e922fff | 2006-03-17 17:27:56 +1100 | [diff] [blame] | 430 | for (low = 0, high = be32_to_cpu(btp->count) - 1; low <= high; ) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | mid = (low + high) >> 1; |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 432 | if ((hash = be32_to_cpu(blp[mid].hashval)) == args->hashval) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | break; |
| 434 | if (hash < args->hashval) |
| 435 | low = mid + 1; |
| 436 | else |
| 437 | high = mid - 1; |
| 438 | } |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 439 | while (mid >= 0 && be32_to_cpu(blp[mid].hashval) >= args->hashval) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | mid--; |
| 441 | } |
| 442 | /* |
| 443 | * No stale entries, will use enddup space to hold new leaf. |
| 444 | */ |
| 445 | if (!btp->stale) { |
| 446 | /* |
| 447 | * Mark the space needed for the new leaf entry, now in use. |
| 448 | */ |
| 449 | xfs_dir2_data_use_free(tp, bp, enddup, |
| 450 | (xfs_dir2_data_aoff_t) |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 451 | ((char *)enddup - (char *)hdr + be16_to_cpu(enddup->length) - |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | sizeof(*blp)), |
| 453 | (xfs_dir2_data_aoff_t)sizeof(*blp), |
| 454 | &needlog, &needscan); |
| 455 | /* |
| 456 | * Update the tail (entry count). |
| 457 | */ |
Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 458 | be32_add_cpu(&btp->count, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | /* |
| 460 | * If we now need to rebuild the bestfree map, do so. |
| 461 | * This needs to happen before the next call to use_free. |
| 462 | */ |
| 463 | if (needscan) { |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 464 | xfs_dir2_data_freescan(mp, hdr, &needlog); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | needscan = 0; |
| 466 | } |
| 467 | /* |
| 468 | * Adjust pointer to the first leaf entry, we're about to move |
| 469 | * the table up one to open up space for the new leaf entry. |
| 470 | * Then adjust our index to match. |
| 471 | */ |
| 472 | blp--; |
| 473 | mid++; |
| 474 | if (mid) |
| 475 | memmove(blp, &blp[1], mid * sizeof(*blp)); |
| 476 | lfloglow = 0; |
| 477 | lfloghigh = mid; |
| 478 | } |
| 479 | /* |
| 480 | * Use a stale leaf for our new entry. |
| 481 | */ |
| 482 | else { |
| 483 | for (lowstale = mid; |
| 484 | lowstale >= 0 && |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 485 | blp[lowstale].address != |
| 486 | cpu_to_be32(XFS_DIR2_NULL_DATAPTR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | lowstale--) |
| 488 | continue; |
| 489 | for (highstale = mid + 1; |
Nathan Scott | e922fff | 2006-03-17 17:27:56 +1100 | [diff] [blame] | 490 | highstale < be32_to_cpu(btp->count) && |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 491 | blp[highstale].address != |
| 492 | cpu_to_be32(XFS_DIR2_NULL_DATAPTR) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | (lowstale < 0 || mid - lowstale > highstale - mid); |
| 494 | highstale++) |
| 495 | continue; |
| 496 | /* |
| 497 | * Move entries toward the low-numbered stale entry. |
| 498 | */ |
| 499 | if (lowstale >= 0 && |
Nathan Scott | e922fff | 2006-03-17 17:27:56 +1100 | [diff] [blame] | 500 | (highstale == be32_to_cpu(btp->count) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | mid - lowstale <= highstale - mid)) { |
| 502 | if (mid - lowstale) |
| 503 | memmove(&blp[lowstale], &blp[lowstale + 1], |
| 504 | (mid - lowstale) * sizeof(*blp)); |
| 505 | lfloglow = MIN(lowstale, lfloglow); |
| 506 | lfloghigh = MAX(mid, lfloghigh); |
| 507 | } |
| 508 | /* |
| 509 | * Move entries toward the high-numbered stale entry. |
| 510 | */ |
| 511 | else { |
Nathan Scott | e922fff | 2006-03-17 17:27:56 +1100 | [diff] [blame] | 512 | ASSERT(highstale < be32_to_cpu(btp->count)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | mid++; |
| 514 | if (highstale - mid) |
| 515 | memmove(&blp[mid + 1], &blp[mid], |
| 516 | (highstale - mid) * sizeof(*blp)); |
| 517 | lfloglow = MIN(mid, lfloglow); |
| 518 | lfloghigh = MAX(highstale, lfloghigh); |
| 519 | } |
Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 520 | be32_add_cpu(&btp->stale, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | } |
| 522 | /* |
| 523 | * Point to the new data entry. |
| 524 | */ |
| 525 | dep = (xfs_dir2_data_entry_t *)dup; |
| 526 | /* |
| 527 | * Fill in the leaf entry. |
| 528 | */ |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 529 | blp[mid].hashval = cpu_to_be32(args->hashval); |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 530 | blp[mid].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp, |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 531 | (char *)dep - (char *)hdr)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | xfs_dir2_block_log_leaf(tp, bp, lfloglow, lfloghigh); |
| 533 | /* |
| 534 | * Mark space for the data entry used. |
| 535 | */ |
| 536 | xfs_dir2_data_use_free(tp, bp, dup, |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 537 | (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | (xfs_dir2_data_aoff_t)len, &needlog, &needscan); |
| 539 | /* |
| 540 | * Create the new data entry. |
| 541 | */ |
Christoph Hellwig | ff9901c | 2006-06-09 14:48:37 +1000 | [diff] [blame] | 542 | dep->inumber = cpu_to_be64(args->inumber); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | dep->namelen = args->namelen; |
| 544 | memcpy(dep->name, args->name, args->namelen); |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 545 | tagp = xfs_dir2_data_entry_tag_p(dep); |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 546 | *tagp = cpu_to_be16((char *)dep - (char *)hdr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | /* |
| 548 | * Clean up the bestfree array and log the header, tail, and entry. |
| 549 | */ |
| 550 | if (needscan) |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 551 | xfs_dir2_data_freescan(mp, hdr, &needlog); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | if (needlog) |
| 553 | xfs_dir2_data_log_header(tp, bp); |
| 554 | xfs_dir2_block_log_tail(tp, bp); |
| 555 | xfs_dir2_data_log_entry(tp, bp, dep); |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame^] | 556 | xfs_dir3_data_check(dp, bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | return 0; |
| 558 | } |
| 559 | |
| 560 | /* |
| 561 | * Readdir for block directories. |
| 562 | */ |
| 563 | int /* error */ |
| 564 | xfs_dir2_block_getdents( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | xfs_inode_t *dp, /* incore inode */ |
Christoph Hellwig | 051e7cd | 2007-08-28 13:58:24 +1000 | [diff] [blame] | 566 | void *dirent, |
| 567 | xfs_off_t *offset, |
| 568 | filldir_t filldir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | { |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 570 | xfs_dir2_data_hdr_t *hdr; /* block header */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 571 | struct xfs_buf *bp; /* buffer for block */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | xfs_dir2_block_tail_t *btp; /* block tail */ |
| 573 | xfs_dir2_data_entry_t *dep; /* block data entry */ |
| 574 | xfs_dir2_data_unused_t *dup; /* block unused entry */ |
| 575 | char *endptr; /* end of the data entries */ |
| 576 | int error; /* error return value */ |
| 577 | xfs_mount_t *mp; /* filesystem mount point */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | char *ptr; /* current data entry */ |
| 579 | int wantoff; /* starting block offset */ |
Christoph Hellwig | 051e7cd | 2007-08-28 13:58:24 +1000 | [diff] [blame] | 580 | xfs_off_t cook; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | |
| 582 | mp = dp->i_mount; |
| 583 | /* |
| 584 | * If the block number in the offset is out of range, we're done. |
| 585 | */ |
Dave Chinner | 20f7e9f | 2012-11-12 22:54:11 +1100 | [diff] [blame] | 586 | if (xfs_dir2_dataptr_to_db(mp, *offset) > mp->m_dirdatablk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | return 0; |
Dave Chinner | 20f7e9f | 2012-11-12 22:54:11 +1100 | [diff] [blame] | 588 | |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 589 | error = xfs_dir3_block_read(NULL, dp, &bp); |
Christoph Hellwig | 051e7cd | 2007-08-28 13:58:24 +1000 | [diff] [blame] | 590 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | return error; |
Christoph Hellwig | 051e7cd | 2007-08-28 13:58:24 +1000 | [diff] [blame] | 592 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | /* |
| 594 | * Extract the byte offset we start at from the seek pointer. |
| 595 | * We'll skip entries before this. |
| 596 | */ |
Christoph Hellwig | 051e7cd | 2007-08-28 13:58:24 +1000 | [diff] [blame] | 597 | wantoff = xfs_dir2_dataptr_to_off(mp, *offset); |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 598 | hdr = bp->b_addr; |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame^] | 599 | xfs_dir3_data_check(dp, bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | /* |
| 601 | * Set up values for the loop. |
| 602 | */ |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 603 | btp = xfs_dir2_block_tail_p(mp, hdr); |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 604 | ptr = (char *)xfs_dir3_data_entry_p(hdr); |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 605 | endptr = (char *)xfs_dir2_block_leaf_p(btp); |
Christoph Hellwig | 051e7cd | 2007-08-28 13:58:24 +1000 | [diff] [blame] | 606 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | /* |
| 608 | * Loop over the data portion of the block. |
| 609 | * Each object is a real entry (dep) or an unused one (dup). |
| 610 | */ |
| 611 | while (ptr < endptr) { |
| 612 | dup = (xfs_dir2_data_unused_t *)ptr; |
| 613 | /* |
| 614 | * Unused, skip it. |
| 615 | */ |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 616 | if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) { |
| 617 | ptr += be16_to_cpu(dup->length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | continue; |
| 619 | } |
| 620 | |
| 621 | dep = (xfs_dir2_data_entry_t *)ptr; |
| 622 | |
| 623 | /* |
| 624 | * Bump pointer for the next iteration. |
| 625 | */ |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 626 | ptr += xfs_dir2_data_entsize(dep->namelen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | /* |
| 628 | * The entry is before the desired starting point, skip it. |
| 629 | */ |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 630 | if ((char *)dep - (char *)hdr < wantoff) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | |
Christoph Hellwig | 051e7cd | 2007-08-28 13:58:24 +1000 | [diff] [blame] | 633 | cook = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk, |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 634 | (char *)dep - (char *)hdr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | |
| 636 | /* |
| 637 | * If it didn't fit, set the final offset to here & return. |
| 638 | */ |
Dave Chinner | 4a24cb7 | 2010-01-20 10:48:05 +1100 | [diff] [blame] | 639 | if (filldir(dirent, (char *)dep->name, dep->namelen, |
| 640 | cook & 0x7fffffff, be64_to_cpu(dep->inumber), |
| 641 | DT_UNKNOWN)) { |
Christoph Hellwig | 1544031 | 2009-01-08 14:00:00 -0500 | [diff] [blame] | 642 | *offset = cook & 0x7fffffff; |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 643 | xfs_trans_brelse(NULL, bp); |
Christoph Hellwig | 051e7cd | 2007-08-28 13:58:24 +1000 | [diff] [blame] | 644 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | } |
| 646 | } |
| 647 | |
| 648 | /* |
| 649 | * Reached the end of the block. |
Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 650 | * Set the offset to a non-existent block 1 and return. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | */ |
Christoph Hellwig | 1544031 | 2009-01-08 14:00:00 -0500 | [diff] [blame] | 652 | *offset = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk + 1, 0) & |
| 653 | 0x7fffffff; |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 654 | xfs_trans_brelse(NULL, bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | return 0; |
| 656 | } |
| 657 | |
| 658 | /* |
| 659 | * Log leaf entries from the block. |
| 660 | */ |
| 661 | static void |
| 662 | xfs_dir2_block_log_leaf( |
| 663 | xfs_trans_t *tp, /* transaction structure */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 664 | struct xfs_buf *bp, /* block buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | int first, /* index of first logged leaf */ |
| 666 | int last) /* index of last logged leaf */ |
| 667 | { |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 668 | xfs_dir2_data_hdr_t *hdr = bp->b_addr; |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 669 | xfs_dir2_leaf_entry_t *blp; |
| 670 | xfs_dir2_block_tail_t *btp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 672 | btp = xfs_dir2_block_tail_p(tp->t_mountp, hdr); |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 673 | blp = xfs_dir2_block_leaf_p(btp); |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 674 | xfs_trans_log_buf(tp, bp, (uint)((char *)&blp[first] - (char *)hdr), |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 675 | (uint)((char *)&blp[last + 1] - (char *)hdr - 1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | /* |
| 679 | * Log the block tail. |
| 680 | */ |
| 681 | static void |
| 682 | xfs_dir2_block_log_tail( |
| 683 | xfs_trans_t *tp, /* transaction structure */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 684 | struct xfs_buf *bp) /* block buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | { |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 686 | xfs_dir2_data_hdr_t *hdr = bp->b_addr; |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 687 | xfs_dir2_block_tail_t *btp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 689 | btp = xfs_dir2_block_tail_p(tp->t_mountp, hdr); |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 690 | xfs_trans_log_buf(tp, bp, (uint)((char *)btp - (char *)hdr), |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 691 | (uint)((char *)(btp + 1) - (char *)hdr - 1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | /* |
| 695 | * Look up an entry in the block. This is the external routine, |
| 696 | * xfs_dir2_block_lookup_int does the real work. |
| 697 | */ |
| 698 | int /* error */ |
| 699 | xfs_dir2_block_lookup( |
| 700 | xfs_da_args_t *args) /* dir lookup arguments */ |
| 701 | { |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 702 | xfs_dir2_data_hdr_t *hdr; /* block header */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | xfs_dir2_leaf_entry_t *blp; /* block leaf entries */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 704 | struct xfs_buf *bp; /* block buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | xfs_dir2_block_tail_t *btp; /* block tail */ |
| 706 | xfs_dir2_data_entry_t *dep; /* block data entry */ |
| 707 | xfs_inode_t *dp; /* incore inode */ |
| 708 | int ent; /* entry index */ |
| 709 | int error; /* error return value */ |
| 710 | xfs_mount_t *mp; /* filesystem mount point */ |
| 711 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 712 | trace_xfs_dir2_block_lookup(args); |
| 713 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | /* |
| 715 | * Get the buffer, look up the entry. |
| 716 | * If not found (ENOENT) then return, have no buffer. |
| 717 | */ |
| 718 | if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) |
| 719 | return error; |
| 720 | dp = args->dp; |
| 721 | mp = dp->i_mount; |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 722 | hdr = bp->b_addr; |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame^] | 723 | xfs_dir3_data_check(dp, bp); |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 724 | btp = xfs_dir2_block_tail_p(mp, hdr); |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 725 | blp = xfs_dir2_block_leaf_p(btp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | /* |
| 727 | * Get the offset from the leaf entry, to point to the data. |
| 728 | */ |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 729 | dep = (xfs_dir2_data_entry_t *)((char *)hdr + |
Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame] | 730 | xfs_dir2_dataptr_to_off(mp, be32_to_cpu(blp[ent].address))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | /* |
Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame] | 732 | * Fill in inode number, CI name if appropriate, release the block. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | */ |
Christoph Hellwig | ff9901c | 2006-06-09 14:48:37 +1000 | [diff] [blame] | 734 | args->inumber = be64_to_cpu(dep->inumber); |
Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame] | 735 | error = xfs_dir_cilookup_result(args, dep->name, dep->namelen); |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 736 | xfs_trans_brelse(args->trans, bp); |
Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame] | 737 | return XFS_ERROR(error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | } |
| 739 | |
| 740 | /* |
| 741 | * Internal block lookup routine. |
| 742 | */ |
| 743 | static int /* error */ |
| 744 | xfs_dir2_block_lookup_int( |
| 745 | xfs_da_args_t *args, /* dir lookup arguments */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 746 | struct xfs_buf **bpp, /* returned block buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | int *entno) /* returned entry number */ |
| 748 | { |
| 749 | xfs_dir2_dataptr_t addr; /* data entry address */ |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 750 | xfs_dir2_data_hdr_t *hdr; /* block header */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | xfs_dir2_leaf_entry_t *blp; /* block leaf entries */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 752 | struct xfs_buf *bp; /* block buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | xfs_dir2_block_tail_t *btp; /* block tail */ |
| 754 | xfs_dir2_data_entry_t *dep; /* block data entry */ |
| 755 | xfs_inode_t *dp; /* incore inode */ |
| 756 | int error; /* error return value */ |
| 757 | xfs_dahash_t hash; /* found hash value */ |
| 758 | int high; /* binary search high index */ |
| 759 | int low; /* binary search low index */ |
| 760 | int mid; /* binary search current idx */ |
| 761 | xfs_mount_t *mp; /* filesystem mount point */ |
| 762 | xfs_trans_t *tp; /* transaction pointer */ |
Barry Naujok | 5163f95 | 2008-05-21 16:41:01 +1000 | [diff] [blame] | 763 | enum xfs_dacmp cmp; /* comparison result */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | |
| 765 | dp = args->dp; |
| 766 | tp = args->trans; |
| 767 | mp = dp->i_mount; |
Dave Chinner | 20f7e9f | 2012-11-12 22:54:11 +1100 | [diff] [blame] | 768 | |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 769 | error = xfs_dir3_block_read(tp, dp, &bp); |
Dave Chinner | 4bb20a8 | 2012-11-12 22:54:10 +1100 | [diff] [blame] | 770 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | return error; |
Dave Chinner | 20f7e9f | 2012-11-12 22:54:11 +1100 | [diff] [blame] | 772 | |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 773 | hdr = bp->b_addr; |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame^] | 774 | xfs_dir3_data_check(dp, bp); |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 775 | btp = xfs_dir2_block_tail_p(mp, hdr); |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 776 | blp = xfs_dir2_block_leaf_p(btp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | /* |
| 778 | * Loop doing a binary search for our hash value. |
| 779 | * Find our entry, ENOENT if it's not there. |
| 780 | */ |
Nathan Scott | e922fff | 2006-03-17 17:27:56 +1100 | [diff] [blame] | 781 | for (low = 0, high = be32_to_cpu(btp->count) - 1; ; ) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | ASSERT(low <= high); |
| 783 | mid = (low + high) >> 1; |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 784 | if ((hash = be32_to_cpu(blp[mid].hashval)) == args->hashval) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | break; |
| 786 | if (hash < args->hashval) |
| 787 | low = mid + 1; |
| 788 | else |
| 789 | high = mid - 1; |
| 790 | if (low > high) { |
Barry Naujok | 6a17810 | 2008-05-21 16:42:05 +1000 | [diff] [blame] | 791 | ASSERT(args->op_flags & XFS_DA_OP_OKNOENT); |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 792 | xfs_trans_brelse(tp, bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | return XFS_ERROR(ENOENT); |
| 794 | } |
| 795 | } |
| 796 | /* |
| 797 | * Back up to the first one with the right hash value. |
| 798 | */ |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 799 | while (mid > 0 && be32_to_cpu(blp[mid - 1].hashval) == args->hashval) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | mid--; |
| 801 | } |
| 802 | /* |
| 803 | * Now loop forward through all the entries with the |
| 804 | * right hash value looking for our name. |
| 805 | */ |
| 806 | do { |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 807 | if ((addr = be32_to_cpu(blp[mid].address)) == XFS_DIR2_NULL_DATAPTR) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | continue; |
| 809 | /* |
| 810 | * Get pointer to the entry from the leaf. |
| 811 | */ |
| 812 | dep = (xfs_dir2_data_entry_t *) |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 813 | ((char *)hdr + xfs_dir2_dataptr_to_off(mp, addr)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | /* |
Barry Naujok | 5163f95 | 2008-05-21 16:41:01 +1000 | [diff] [blame] | 815 | * Compare name and if it's an exact match, return the index |
| 816 | * and buffer. If it's the first case-insensitive match, store |
| 817 | * the index and buffer and continue looking for an exact match. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | */ |
Barry Naujok | 5163f95 | 2008-05-21 16:41:01 +1000 | [diff] [blame] | 819 | cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen); |
| 820 | if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) { |
| 821 | args->cmpresult = cmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | *bpp = bp; |
| 823 | *entno = mid; |
Barry Naujok | 5163f95 | 2008-05-21 16:41:01 +1000 | [diff] [blame] | 824 | if (cmp == XFS_CMP_EXACT) |
| 825 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 | } |
Barry Naujok | 5163f95 | 2008-05-21 16:41:01 +1000 | [diff] [blame] | 827 | } while (++mid < be32_to_cpu(btp->count) && |
| 828 | be32_to_cpu(blp[mid].hashval) == hash); |
| 829 | |
Barry Naujok | 6a17810 | 2008-05-21 16:42:05 +1000 | [diff] [blame] | 830 | ASSERT(args->op_flags & XFS_DA_OP_OKNOENT); |
Barry Naujok | 5163f95 | 2008-05-21 16:41:01 +1000 | [diff] [blame] | 831 | /* |
| 832 | * Here, we can only be doing a lookup (not a rename or replace). |
| 833 | * If a case-insensitive match was found earlier, return success. |
| 834 | */ |
| 835 | if (args->cmpresult == XFS_CMP_CASE) |
| 836 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | /* |
| 838 | * No match, release the buffer and return ENOENT. |
| 839 | */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 840 | xfs_trans_brelse(tp, bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 841 | return XFS_ERROR(ENOENT); |
| 842 | } |
| 843 | |
| 844 | /* |
| 845 | * Remove an entry from a block format directory. |
| 846 | * If that makes the block small enough to fit in shortform, transform it. |
| 847 | */ |
| 848 | int /* error */ |
| 849 | xfs_dir2_block_removename( |
| 850 | xfs_da_args_t *args) /* directory operation args */ |
| 851 | { |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 852 | xfs_dir2_data_hdr_t *hdr; /* block header */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | xfs_dir2_leaf_entry_t *blp; /* block leaf pointer */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 854 | struct xfs_buf *bp; /* block buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | xfs_dir2_block_tail_t *btp; /* block tail */ |
| 856 | xfs_dir2_data_entry_t *dep; /* block data entry */ |
| 857 | xfs_inode_t *dp; /* incore inode */ |
| 858 | int ent; /* block leaf entry index */ |
| 859 | int error; /* error return value */ |
| 860 | xfs_mount_t *mp; /* filesystem mount point */ |
| 861 | int needlog; /* need to log block header */ |
| 862 | int needscan; /* need to fixup bestfree */ |
| 863 | xfs_dir2_sf_hdr_t sfh; /* shortform header */ |
| 864 | int size; /* shortform size */ |
| 865 | xfs_trans_t *tp; /* transaction pointer */ |
| 866 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 867 | trace_xfs_dir2_block_removename(args); |
| 868 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | /* |
| 870 | * Look up the entry in the block. Gets the buffer and entry index. |
| 871 | * It will always be there, the vnodeops level does a lookup first. |
| 872 | */ |
| 873 | if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) { |
| 874 | return error; |
| 875 | } |
| 876 | dp = args->dp; |
| 877 | tp = args->trans; |
| 878 | mp = dp->i_mount; |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 879 | hdr = bp->b_addr; |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 880 | btp = xfs_dir2_block_tail_p(mp, hdr); |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 881 | blp = xfs_dir2_block_leaf_p(btp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 882 | /* |
| 883 | * Point to the data entry using the leaf entry. |
| 884 | */ |
| 885 | dep = (xfs_dir2_data_entry_t *) |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 886 | ((char *)hdr + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(blp[ent].address))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | /* |
| 888 | * Mark the data entry's space free. |
| 889 | */ |
| 890 | needlog = needscan = 0; |
| 891 | xfs_dir2_data_make_free(tp, bp, |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 892 | (xfs_dir2_data_aoff_t)((char *)dep - (char *)hdr), |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 893 | xfs_dir2_data_entsize(dep->namelen), &needlog, &needscan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 | /* |
| 895 | * Fix up the block tail. |
| 896 | */ |
Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 897 | be32_add_cpu(&btp->stale, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | xfs_dir2_block_log_tail(tp, bp); |
| 899 | /* |
| 900 | * Remove the leaf entry by marking it stale. |
| 901 | */ |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 902 | blp[ent].address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | xfs_dir2_block_log_leaf(tp, bp, ent, ent); |
| 904 | /* |
| 905 | * Fix up bestfree, log the header if necessary. |
| 906 | */ |
| 907 | if (needscan) |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 908 | xfs_dir2_data_freescan(mp, hdr, &needlog); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 909 | if (needlog) |
| 910 | xfs_dir2_data_log_header(tp, bp); |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame^] | 911 | xfs_dir3_data_check(dp, bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 912 | /* |
| 913 | * See if the size as a shortform is good enough. |
| 914 | */ |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 915 | size = xfs_dir2_block_sfsize(dp, hdr, &sfh); |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 916 | if (size > XFS_IFORK_DSIZE(dp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | return 0; |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 918 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | /* |
| 920 | * If it works, do the conversion. |
| 921 | */ |
| 922 | return xfs_dir2_block_to_sf(args, bp, size, &sfh); |
| 923 | } |
| 924 | |
| 925 | /* |
| 926 | * Replace an entry in a V2 block directory. |
| 927 | * Change the inode number to the new value. |
| 928 | */ |
| 929 | int /* error */ |
| 930 | xfs_dir2_block_replace( |
| 931 | xfs_da_args_t *args) /* directory operation args */ |
| 932 | { |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 933 | xfs_dir2_data_hdr_t *hdr; /* block header */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 934 | xfs_dir2_leaf_entry_t *blp; /* block leaf entries */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 935 | struct xfs_buf *bp; /* block buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 936 | xfs_dir2_block_tail_t *btp; /* block tail */ |
| 937 | xfs_dir2_data_entry_t *dep; /* block data entry */ |
| 938 | xfs_inode_t *dp; /* incore inode */ |
| 939 | int ent; /* leaf entry index */ |
| 940 | int error; /* error return value */ |
| 941 | xfs_mount_t *mp; /* filesystem mount point */ |
| 942 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 943 | trace_xfs_dir2_block_replace(args); |
| 944 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | /* |
| 946 | * Lookup the entry in the directory. Get buffer and entry index. |
| 947 | * This will always succeed since the caller has already done a lookup. |
| 948 | */ |
| 949 | if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) { |
| 950 | return error; |
| 951 | } |
| 952 | dp = args->dp; |
| 953 | mp = dp->i_mount; |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 954 | hdr = bp->b_addr; |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 955 | btp = xfs_dir2_block_tail_p(mp, hdr); |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 956 | blp = xfs_dir2_block_leaf_p(btp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | /* |
| 958 | * Point to the data entry we need to change. |
| 959 | */ |
| 960 | dep = (xfs_dir2_data_entry_t *) |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 961 | ((char *)hdr + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(blp[ent].address))); |
Christoph Hellwig | ff9901c | 2006-06-09 14:48:37 +1000 | [diff] [blame] | 962 | ASSERT(be64_to_cpu(dep->inumber) != args->inumber); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 963 | /* |
| 964 | * Change the inode number to the new value. |
| 965 | */ |
Christoph Hellwig | ff9901c | 2006-06-09 14:48:37 +1000 | [diff] [blame] | 966 | dep->inumber = cpu_to_be64(args->inumber); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | xfs_dir2_data_log_entry(args->trans, bp, dep); |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame^] | 968 | xfs_dir3_data_check(dp, bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 969 | return 0; |
| 970 | } |
| 971 | |
| 972 | /* |
| 973 | * Qsort comparison routine for the block leaf entries. |
| 974 | */ |
| 975 | static int /* sort order */ |
| 976 | xfs_dir2_block_sort( |
| 977 | const void *a, /* first leaf entry */ |
| 978 | const void *b) /* second leaf entry */ |
| 979 | { |
| 980 | const xfs_dir2_leaf_entry_t *la; /* first leaf entry */ |
| 981 | const xfs_dir2_leaf_entry_t *lb; /* second leaf entry */ |
| 982 | |
| 983 | la = a; |
| 984 | lb = b; |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 985 | return be32_to_cpu(la->hashval) < be32_to_cpu(lb->hashval) ? -1 : |
| 986 | (be32_to_cpu(la->hashval) > be32_to_cpu(lb->hashval) ? 1 : 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 987 | } |
| 988 | |
| 989 | /* |
| 990 | * Convert a V2 leaf directory to a V2 block directory if possible. |
| 991 | */ |
| 992 | int /* error */ |
| 993 | xfs_dir2_leaf_to_block( |
| 994 | xfs_da_args_t *args, /* operation arguments */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 995 | struct xfs_buf *lbp, /* leaf buffer */ |
| 996 | struct xfs_buf *dbp) /* data buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 997 | { |
Nathan Scott | 68b3a10 | 2006-03-17 17:27:19 +1100 | [diff] [blame] | 998 | __be16 *bestsp; /* leaf bests table */ |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 999 | xfs_dir2_data_hdr_t *hdr; /* block header */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1000 | xfs_dir2_block_tail_t *btp; /* block tail */ |
| 1001 | xfs_inode_t *dp; /* incore directory inode */ |
| 1002 | xfs_dir2_data_unused_t *dup; /* unused data entry */ |
| 1003 | int error; /* error return value */ |
| 1004 | int from; /* leaf from index */ |
| 1005 | xfs_dir2_leaf_t *leaf; /* leaf structure */ |
| 1006 | xfs_dir2_leaf_entry_t *lep; /* leaf entry */ |
| 1007 | xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */ |
| 1008 | xfs_mount_t *mp; /* file system mount point */ |
| 1009 | int needlog; /* need to log data header */ |
| 1010 | int needscan; /* need to scan for bestfree */ |
| 1011 | xfs_dir2_sf_hdr_t sfh; /* shortform header */ |
| 1012 | int size; /* bytes used */ |
Nathan Scott | 3d693c6 | 2006-03-17 17:28:27 +1100 | [diff] [blame] | 1013 | __be16 *tagp; /* end of entry (tag) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1014 | int to; /* block/leaf to index */ |
| 1015 | xfs_trans_t *tp; /* transaction pointer */ |
| 1016 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1017 | trace_xfs_dir2_leaf_to_block(args); |
| 1018 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1019 | dp = args->dp; |
| 1020 | tp = args->trans; |
| 1021 | mp = dp->i_mount; |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1022 | leaf = lbp->b_addr; |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 1023 | ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC)); |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1024 | ltp = xfs_dir2_leaf_tail_p(mp, leaf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 | /* |
| 1026 | * If there are data blocks other than the first one, take this |
| 1027 | * opportunity to remove trailing empty data blocks that may have |
| 1028 | * been left behind during no-space-reservation operations. |
| 1029 | * These will show up in the leaf bests table. |
| 1030 | */ |
| 1031 | while (dp->i_d.di_size > mp->m_dirblksize) { |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 1032 | int hdrsz; |
| 1033 | |
| 1034 | hdrsz = xfs_dir3_data_hdr_size(xfs_sb_version_hascrc(&mp->m_sb)); |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1035 | bestsp = xfs_dir2_leaf_bests_p(ltp); |
Nathan Scott | afbcb3f | 2006-03-17 17:27:28 +1100 | [diff] [blame] | 1036 | if (be16_to_cpu(bestsp[be32_to_cpu(ltp->bestcount) - 1]) == |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 1037 | mp->m_dirblksize - hdrsz) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | if ((error = |
| 1039 | xfs_dir2_leaf_trim_data(args, lbp, |
Nathan Scott | afbcb3f | 2006-03-17 17:27:28 +1100 | [diff] [blame] | 1040 | (xfs_dir2_db_t)(be32_to_cpu(ltp->bestcount) - 1)))) |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1041 | return error; |
| 1042 | } else |
| 1043 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | } |
| 1045 | /* |
| 1046 | * Read the data block if we don't already have it, give up if it fails. |
| 1047 | */ |
Dave Chinner | 4bb20a8 | 2012-11-12 22:54:10 +1100 | [diff] [blame] | 1048 | if (!dbp) { |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame^] | 1049 | error = xfs_dir3_data_read(tp, dp, mp->m_dirdatablk, -1, &dbp); |
Dave Chinner | 4bb20a8 | 2012-11-12 22:54:10 +1100 | [diff] [blame] | 1050 | if (error) |
| 1051 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1052 | } |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1053 | hdr = dbp->b_addr; |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame^] | 1054 | ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) || |
| 1055 | hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC)); |
| 1056 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1057 | /* |
| 1058 | * Size of the "leaf" area in the block. |
| 1059 | */ |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 1060 | size = (uint)sizeof(xfs_dir2_block_tail_t) + |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 1061 | (uint)sizeof(*lep) * (be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1062 | /* |
| 1063 | * Look at the last data entry. |
| 1064 | */ |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 1065 | tagp = (__be16 *)((char *)hdr + mp->m_dirblksize) - 1; |
| 1066 | dup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | /* |
| 1068 | * If it's not free or is too short we can't do it. |
| 1069 | */ |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 1070 | if (be16_to_cpu(dup->freetag) != XFS_DIR2_DATA_FREE_TAG || |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1071 | be16_to_cpu(dup->length) < size) |
| 1072 | return 0; |
| 1073 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1074 | /* |
| 1075 | * Start converting it to block form. |
| 1076 | */ |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 1077 | xfs_dir3_block_init(mp, dbp, dp); |
| 1078 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | needlog = 1; |
| 1080 | needscan = 0; |
| 1081 | /* |
| 1082 | * Use up the space at the end of the block (blp/btp). |
| 1083 | */ |
| 1084 | xfs_dir2_data_use_free(tp, dbp, dup, mp->m_dirblksize - size, size, |
| 1085 | &needlog, &needscan); |
| 1086 | /* |
| 1087 | * Initialize the block tail. |
| 1088 | */ |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 1089 | btp = xfs_dir2_block_tail_p(mp, hdr); |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 1090 | btp->count = cpu_to_be32(be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | btp->stale = 0; |
| 1092 | xfs_dir2_block_log_tail(tp, dbp); |
| 1093 | /* |
| 1094 | * Initialize the block leaf area. We compact out stale entries. |
| 1095 | */ |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1096 | lep = xfs_dir2_block_leaf_p(btp); |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 1097 | for (from = to = 0; from < be16_to_cpu(leaf->hdr.count); from++) { |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 1098 | if (leaf->ents[from].address == |
| 1099 | cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1100 | continue; |
| 1101 | lep[to++] = leaf->ents[from]; |
| 1102 | } |
Nathan Scott | e922fff | 2006-03-17 17:27:56 +1100 | [diff] [blame] | 1103 | ASSERT(to == be32_to_cpu(btp->count)); |
| 1104 | xfs_dir2_block_log_leaf(tp, dbp, 0, be32_to_cpu(btp->count) - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1105 | /* |
| 1106 | * Scan the bestfree if we need it and log the data block header. |
| 1107 | */ |
| 1108 | if (needscan) |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 1109 | xfs_dir2_data_freescan(mp, hdr, &needlog); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1110 | if (needlog) |
| 1111 | xfs_dir2_data_log_header(tp, dbp); |
| 1112 | /* |
| 1113 | * Pitch the old leaf block. |
| 1114 | */ |
| 1115 | error = xfs_da_shrink_inode(args, mp->m_dirleafblk, lbp); |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1116 | if (error) |
| 1117 | return error; |
| 1118 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1119 | /* |
| 1120 | * Now see if the resulting block can be shrunken to shortform. |
| 1121 | */ |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 1122 | size = xfs_dir2_block_sfsize(dp, hdr, &sfh); |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1123 | if (size > XFS_IFORK_DSIZE(dp)) |
| 1124 | return 0; |
| 1125 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1126 | return xfs_dir2_block_to_sf(args, dbp, size, &sfh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1127 | } |
| 1128 | |
| 1129 | /* |
| 1130 | * Convert the shortform directory to block form. |
| 1131 | */ |
| 1132 | int /* error */ |
| 1133 | xfs_dir2_sf_to_block( |
| 1134 | xfs_da_args_t *args) /* operation arguments */ |
| 1135 | { |
| 1136 | xfs_dir2_db_t blkno; /* dir-relative block # (0) */ |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 1137 | xfs_dir2_data_hdr_t *hdr; /* block header */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1138 | xfs_dir2_leaf_entry_t *blp; /* block leaf entries */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1139 | struct xfs_buf *bp; /* block buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1140 | xfs_dir2_block_tail_t *btp; /* block tail pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1141 | xfs_dir2_data_entry_t *dep; /* data entry pointer */ |
| 1142 | xfs_inode_t *dp; /* incore directory inode */ |
| 1143 | int dummy; /* trash */ |
| 1144 | xfs_dir2_data_unused_t *dup; /* unused entry pointer */ |
| 1145 | int endoffset; /* end of data objects */ |
| 1146 | int error; /* error return value */ |
| 1147 | int i; /* index */ |
| 1148 | xfs_mount_t *mp; /* filesystem mount point */ |
| 1149 | int needlog; /* need to log block header */ |
| 1150 | int needscan; /* need to scan block freespc */ |
| 1151 | int newoffset; /* offset from current entry */ |
| 1152 | int offset; /* target block offset */ |
| 1153 | xfs_dir2_sf_entry_t *sfep; /* sf entry pointer */ |
Christoph Hellwig | ac8ba50 | 2011-07-08 14:35:13 +0200 | [diff] [blame] | 1154 | xfs_dir2_sf_hdr_t *oldsfp; /* old shortform header */ |
| 1155 | xfs_dir2_sf_hdr_t *sfp; /* shortform header */ |
Nathan Scott | 3d693c6 | 2006-03-17 17:28:27 +1100 | [diff] [blame] | 1156 | __be16 *tagp; /* end of data entry */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1157 | xfs_trans_t *tp; /* transaction pointer */ |
Barry Naujok | 5163f95 | 2008-05-21 16:41:01 +1000 | [diff] [blame] | 1158 | struct xfs_name name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1159 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1160 | trace_xfs_dir2_sf_to_block(args); |
| 1161 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1162 | dp = args->dp; |
| 1163 | tp = args->trans; |
| 1164 | mp = dp->i_mount; |
| 1165 | ASSERT(dp->i_df.if_flags & XFS_IFINLINE); |
| 1166 | /* |
| 1167 | * Bomb out if the shortform directory is way too short. |
| 1168 | */ |
| 1169 | if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) { |
| 1170 | ASSERT(XFS_FORCED_SHUTDOWN(mp)); |
| 1171 | return XFS_ERROR(EIO); |
| 1172 | } |
Christoph Hellwig | ac8ba50 | 2011-07-08 14:35:13 +0200 | [diff] [blame] | 1173 | |
| 1174 | oldsfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data; |
| 1175 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1176 | ASSERT(dp->i_df.if_bytes == dp->i_d.di_size); |
| 1177 | ASSERT(dp->i_df.if_u1.if_data != NULL); |
Christoph Hellwig | ac8ba50 | 2011-07-08 14:35:13 +0200 | [diff] [blame] | 1178 | ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(oldsfp->i8count)); |
| 1179 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1180 | /* |
Christoph Hellwig | ac8ba50 | 2011-07-08 14:35:13 +0200 | [diff] [blame] | 1181 | * Copy the directory into a temporary buffer. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1182 | * Then pitch the incore inode data so we can make extents. |
| 1183 | */ |
Christoph Hellwig | ac8ba50 | 2011-07-08 14:35:13 +0200 | [diff] [blame] | 1184 | sfp = kmem_alloc(dp->i_df.if_bytes, KM_SLEEP); |
| 1185 | memcpy(sfp, oldsfp, dp->i_df.if_bytes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1186 | |
Christoph Hellwig | ac8ba50 | 2011-07-08 14:35:13 +0200 | [diff] [blame] | 1187 | xfs_idata_realloc(dp, -dp->i_df.if_bytes, XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1188 | dp->i_d.di_size = 0; |
| 1189 | xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE); |
Christoph Hellwig | ac8ba50 | 2011-07-08 14:35:13 +0200 | [diff] [blame] | 1190 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1191 | /* |
| 1192 | * Add block 0 to the inode. |
| 1193 | */ |
| 1194 | error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE, &blkno); |
| 1195 | if (error) { |
Christoph Hellwig | ac8ba50 | 2011-07-08 14:35:13 +0200 | [diff] [blame] | 1196 | kmem_free(sfp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1197 | return error; |
| 1198 | } |
| 1199 | /* |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 1200 | * Initialize the data block, then convert it to block format. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1201 | */ |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 1202 | error = xfs_dir3_data_init(args, blkno, &bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1203 | if (error) { |
Christoph Hellwig | ac8ba50 | 2011-07-08 14:35:13 +0200 | [diff] [blame] | 1204 | kmem_free(sfp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1205 | return error; |
| 1206 | } |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 1207 | xfs_dir3_block_init(mp, bp, dp); |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1208 | hdr = bp->b_addr; |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 1209 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1210 | /* |
| 1211 | * Compute size of block "tail" area. |
| 1212 | */ |
| 1213 | i = (uint)sizeof(*btp) + |
Christoph Hellwig | ac8ba50 | 2011-07-08 14:35:13 +0200 | [diff] [blame] | 1214 | (sfp->count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1215 | /* |
| 1216 | * The whole thing is initialized to free by the init routine. |
| 1217 | * Say we're using the leaf and tail area. |
| 1218 | */ |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 1219 | dup = xfs_dir3_data_unused_p(hdr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1220 | needlog = needscan = 0; |
| 1221 | xfs_dir2_data_use_free(tp, bp, dup, mp->m_dirblksize - i, i, &needlog, |
| 1222 | &needscan); |
| 1223 | ASSERT(needscan == 0); |
| 1224 | /* |
| 1225 | * Fill in the tail. |
| 1226 | */ |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 1227 | btp = xfs_dir2_block_tail_p(mp, hdr); |
Christoph Hellwig | ac8ba50 | 2011-07-08 14:35:13 +0200 | [diff] [blame] | 1228 | btp->count = cpu_to_be32(sfp->count + 2); /* ., .. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1229 | btp->stale = 0; |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1230 | blp = xfs_dir2_block_leaf_p(btp); |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 1231 | endoffset = (uint)((char *)blp - (char *)hdr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1232 | /* |
| 1233 | * Remove the freespace, we'll manage it. |
| 1234 | */ |
| 1235 | xfs_dir2_data_use_free(tp, bp, dup, |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 1236 | (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr), |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 1237 | be16_to_cpu(dup->length), &needlog, &needscan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1238 | /* |
| 1239 | * Create entry for . |
| 1240 | */ |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 1241 | dep = xfs_dir3_data_dot_entry_p(hdr); |
Christoph Hellwig | ff9901c | 2006-06-09 14:48:37 +1000 | [diff] [blame] | 1242 | dep->inumber = cpu_to_be64(dp->i_ino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1243 | dep->namelen = 1; |
| 1244 | dep->name[0] = '.'; |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1245 | tagp = xfs_dir2_data_entry_tag_p(dep); |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 1246 | *tagp = cpu_to_be16((char *)dep - (char *)hdr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1247 | xfs_dir2_data_log_entry(tp, bp, dep); |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 1248 | blp[0].hashval = cpu_to_be32(xfs_dir_hash_dot); |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1249 | blp[0].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp, |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 1250 | (char *)dep - (char *)hdr)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1251 | /* |
| 1252 | * Create entry for .. |
| 1253 | */ |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 1254 | dep = xfs_dir3_data_dotdot_entry_p(hdr); |
Christoph Hellwig | 8bc3878 | 2011-07-08 14:35:03 +0200 | [diff] [blame] | 1255 | dep->inumber = cpu_to_be64(xfs_dir2_sf_get_parent_ino(sfp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1256 | dep->namelen = 2; |
| 1257 | dep->name[0] = dep->name[1] = '.'; |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1258 | tagp = xfs_dir2_data_entry_tag_p(dep); |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 1259 | *tagp = cpu_to_be16((char *)dep - (char *)hdr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1260 | xfs_dir2_data_log_entry(tp, bp, dep); |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 1261 | blp[1].hashval = cpu_to_be32(xfs_dir_hash_dotdot); |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1262 | blp[1].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp, |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 1263 | (char *)dep - (char *)hdr)); |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 1264 | offset = xfs_dir3_data_first_offset(hdr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1265 | /* |
| 1266 | * Loop over existing entries, stuff them in. |
| 1267 | */ |
Christoph Hellwig | ac8ba50 | 2011-07-08 14:35:13 +0200 | [diff] [blame] | 1268 | i = 0; |
| 1269 | if (!sfp->count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1270 | sfep = NULL; |
| 1271 | else |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1272 | sfep = xfs_dir2_sf_firstentry(sfp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1273 | /* |
| 1274 | * Need to preserve the existing offset values in the sf directory. |
| 1275 | * Insert holes (unused entries) where necessary. |
| 1276 | */ |
| 1277 | while (offset < endoffset) { |
| 1278 | /* |
| 1279 | * sfep is null when we reach the end of the list. |
| 1280 | */ |
| 1281 | if (sfep == NULL) |
| 1282 | newoffset = endoffset; |
| 1283 | else |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1284 | newoffset = xfs_dir2_sf_get_offset(sfep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1285 | /* |
| 1286 | * There should be a hole here, make one. |
| 1287 | */ |
| 1288 | if (offset < newoffset) { |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 1289 | dup = (xfs_dir2_data_unused_t *)((char *)hdr + offset); |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 1290 | dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG); |
| 1291 | dup->length = cpu_to_be16(newoffset - offset); |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1292 | *xfs_dir2_data_unused_tag_p(dup) = cpu_to_be16( |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 1293 | ((char *)dup - (char *)hdr)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1294 | xfs_dir2_data_log_unused(tp, bp, dup); |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 1295 | xfs_dir2_data_freeinsert(hdr, dup, &dummy); |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 1296 | offset += be16_to_cpu(dup->length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1297 | continue; |
| 1298 | } |
| 1299 | /* |
| 1300 | * Copy a real entry. |
| 1301 | */ |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 1302 | dep = (xfs_dir2_data_entry_t *)((char *)hdr + newoffset); |
Christoph Hellwig | 8bc3878 | 2011-07-08 14:35:03 +0200 | [diff] [blame] | 1303 | dep->inumber = cpu_to_be64(xfs_dir2_sfe_get_ino(sfp, sfep)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1304 | dep->namelen = sfep->namelen; |
| 1305 | memcpy(dep->name, sfep->name, dep->namelen); |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1306 | tagp = xfs_dir2_data_entry_tag_p(dep); |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 1307 | *tagp = cpu_to_be16((char *)dep - (char *)hdr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1308 | xfs_dir2_data_log_entry(tp, bp, dep); |
Barry Naujok | 5163f95 | 2008-05-21 16:41:01 +1000 | [diff] [blame] | 1309 | name.name = sfep->name; |
| 1310 | name.len = sfep->namelen; |
| 1311 | blp[2 + i].hashval = cpu_to_be32(mp->m_dirnameops-> |
| 1312 | hashname(&name)); |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1313 | blp[2 + i].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp, |
Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 1314 | (char *)dep - (char *)hdr)); |
| 1315 | offset = (int)((char *)(tagp + 1) - (char *)hdr); |
Christoph Hellwig | ac8ba50 | 2011-07-08 14:35:13 +0200 | [diff] [blame] | 1316 | if (++i == sfp->count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1317 | sfep = NULL; |
| 1318 | else |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1319 | sfep = xfs_dir2_sf_nextentry(sfp, sfep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1320 | } |
| 1321 | /* Done with the temporary buffer */ |
Christoph Hellwig | ac8ba50 | 2011-07-08 14:35:13 +0200 | [diff] [blame] | 1322 | kmem_free(sfp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1323 | /* |
| 1324 | * Sort the leaf entries by hash value. |
| 1325 | */ |
Nathan Scott | e922fff | 2006-03-17 17:27:56 +1100 | [diff] [blame] | 1326 | xfs_sort(blp, be32_to_cpu(btp->count), sizeof(*blp), xfs_dir2_block_sort); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1327 | /* |
| 1328 | * Log the leaf entry area and tail. |
| 1329 | * Already logged the header in data_init, ignore needlog. |
| 1330 | */ |
| 1331 | ASSERT(needscan == 0); |
Nathan Scott | e922fff | 2006-03-17 17:27:56 +1100 | [diff] [blame] | 1332 | xfs_dir2_block_log_leaf(tp, bp, 0, be32_to_cpu(btp->count) - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1333 | xfs_dir2_block_log_tail(tp, bp); |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame^] | 1334 | xfs_dir3_data_check(dp, bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1335 | return 0; |
| 1336 | } |