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