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