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