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-2002,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" |
| 33 | #include "xfs_inode.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include "xfs_dir2_data.h" |
| 35 | #include "xfs_dir2_leaf.h" |
| 36 | #include "xfs_dir2_block.h" |
| 37 | #include "xfs_error.h" |
| 38 | |
| 39 | #ifdef DEBUG |
| 40 | /* |
| 41 | * Check the consistency of the data block. |
| 42 | * The input can also be a block-format directory. |
| 43 | * Pop an assert if we find anything bad. |
| 44 | */ |
| 45 | void |
| 46 | xfs_dir2_data_check( |
| 47 | xfs_inode_t *dp, /* incore inode pointer */ |
| 48 | xfs_dabuf_t *bp) /* data block's buffer */ |
| 49 | { |
| 50 | xfs_dir2_dataptr_t addr; /* addr for leaf lookup */ |
| 51 | xfs_dir2_data_free_t *bf; /* bestfree table */ |
| 52 | xfs_dir2_block_tail_t *btp=NULL; /* block tail */ |
| 53 | int count; /* count of entries found */ |
| 54 | xfs_dir2_data_t *d; /* data block pointer */ |
| 55 | xfs_dir2_data_entry_t *dep; /* data entry */ |
| 56 | xfs_dir2_data_free_t *dfp; /* bestfree entry */ |
| 57 | xfs_dir2_data_unused_t *dup; /* unused entry */ |
| 58 | char *endp; /* end of useful data */ |
| 59 | int freeseen; /* mask of bestfrees seen */ |
| 60 | xfs_dahash_t hash; /* hash of current name */ |
| 61 | int i; /* leaf index */ |
| 62 | int lastfree; /* last entry was unused */ |
| 63 | xfs_dir2_leaf_entry_t *lep=NULL; /* block leaf entries */ |
| 64 | xfs_mount_t *mp; /* filesystem mount point */ |
| 65 | char *p; /* current data position */ |
| 66 | int stale; /* count of stale leaves */ |
| 67 | |
| 68 | mp = dp->i_mount; |
| 69 | d = bp->data; |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 70 | ASSERT(be32_to_cpu(d->hdr.magic) == XFS_DIR2_DATA_MAGIC || |
| 71 | be32_to_cpu(d->hdr.magic) == XFS_DIR2_BLOCK_MAGIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | bf = d->hdr.bestfree; |
| 73 | p = (char *)d->u; |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 74 | if (be32_to_cpu(d->hdr.magic) == XFS_DIR2_BLOCK_MAGIC) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | btp = XFS_DIR2_BLOCK_TAIL_P(mp, (xfs_dir2_block_t *)d); |
| 76 | lep = XFS_DIR2_BLOCK_LEAF_P(btp); |
| 77 | endp = (char *)lep; |
| 78 | } else |
| 79 | endp = (char *)d + mp->m_dirblksize; |
| 80 | count = lastfree = freeseen = 0; |
| 81 | /* |
| 82 | * Account for zero bestfree entries. |
| 83 | */ |
| 84 | if (!bf[0].length) { |
| 85 | ASSERT(!bf[0].offset); |
| 86 | freeseen |= 1 << 0; |
| 87 | } |
| 88 | if (!bf[1].length) { |
| 89 | ASSERT(!bf[1].offset); |
| 90 | freeseen |= 1 << 1; |
| 91 | } |
| 92 | if (!bf[2].length) { |
| 93 | ASSERT(!bf[2].offset); |
| 94 | freeseen |= 1 << 2; |
| 95 | } |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 96 | ASSERT(be16_to_cpu(bf[0].length) >= be16_to_cpu(bf[1].length)); |
| 97 | ASSERT(be16_to_cpu(bf[1].length) >= be16_to_cpu(bf[2].length)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | /* |
| 99 | * Loop over the data/unused entries. |
| 100 | */ |
| 101 | while (p < endp) { |
| 102 | dup = (xfs_dir2_data_unused_t *)p; |
| 103 | /* |
| 104 | * If it's unused, look for the space in the bestfree table. |
| 105 | * If we find it, account for that, else make sure it |
| 106 | * doesn't need to be there. |
| 107 | */ |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 108 | if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | ASSERT(lastfree == 0); |
Nathan Scott | 1fba9f7 | 2006-03-17 17:27:47 +1100 | [diff] [blame] | 110 | ASSERT(be16_to_cpu(*XFS_DIR2_DATA_UNUSED_TAG_P(dup)) == |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | (char *)dup - (char *)d); |
| 112 | dfp = xfs_dir2_data_freefind(d, dup); |
| 113 | if (dfp) { |
| 114 | i = (int)(dfp - bf); |
| 115 | ASSERT((freeseen & (1 << i)) == 0); |
| 116 | freeseen |= 1 << i; |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 117 | } else { |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 118 | ASSERT(be16_to_cpu(dup->length) <= |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 119 | be16_to_cpu(bf[2].length)); |
| 120 | } |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 121 | p += be16_to_cpu(dup->length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | lastfree = 1; |
| 123 | continue; |
| 124 | } |
| 125 | /* |
| 126 | * It's a real entry. Validate the fields. |
| 127 | * If this is a block directory then make sure it's |
| 128 | * in the leaf section of the block. |
| 129 | * The linear search is crude but this is DEBUG code. |
| 130 | */ |
| 131 | dep = (xfs_dir2_data_entry_t *)p; |
| 132 | ASSERT(dep->namelen != 0); |
Christoph Hellwig | ff9901c | 2006-06-09 14:48:37 +1000 | [diff] [blame] | 133 | ASSERT(xfs_dir_ino_validate(mp, be64_to_cpu(dep->inumber)) == 0); |
Nathan Scott | 3d693c6 | 2006-03-17 17:28:27 +1100 | [diff] [blame] | 134 | ASSERT(be16_to_cpu(*XFS_DIR2_DATA_ENTRY_TAG_P(dep)) == |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | (char *)dep - (char *)d); |
| 136 | count++; |
| 137 | lastfree = 0; |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 138 | if (be32_to_cpu(d->hdr.magic) == XFS_DIR2_BLOCK_MAGIC) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | addr = XFS_DIR2_DB_OFF_TO_DATAPTR(mp, mp->m_dirdatablk, |
| 140 | (xfs_dir2_data_aoff_t) |
| 141 | ((char *)dep - (char *)d)); |
| 142 | hash = xfs_da_hashname((char *)dep->name, dep->namelen); |
Nathan Scott | e922fff | 2006-03-17 17:27:56 +1100 | [diff] [blame] | 143 | for (i = 0; i < be32_to_cpu(btp->count); i++) { |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 144 | if (be32_to_cpu(lep[i].address) == addr && |
| 145 | be32_to_cpu(lep[i].hashval) == hash) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | break; |
| 147 | } |
Nathan Scott | e922fff | 2006-03-17 17:27:56 +1100 | [diff] [blame] | 148 | ASSERT(i < be32_to_cpu(btp->count)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | } |
| 150 | p += XFS_DIR2_DATA_ENTSIZE(dep->namelen); |
| 151 | } |
| 152 | /* |
| 153 | * Need to have seen all the entries and all the bestfree slots. |
| 154 | */ |
| 155 | ASSERT(freeseen == 7); |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 156 | if (be32_to_cpu(d->hdr.magic) == XFS_DIR2_BLOCK_MAGIC) { |
Nathan Scott | e922fff | 2006-03-17 17:27:56 +1100 | [diff] [blame] | 157 | for (i = stale = 0; i < be32_to_cpu(btp->count); i++) { |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 158 | if (be32_to_cpu(lep[i].address) == XFS_DIR2_NULL_DATAPTR) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | stale++; |
| 160 | if (i > 0) |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 161 | ASSERT(be32_to_cpu(lep[i].hashval) >= be32_to_cpu(lep[i - 1].hashval)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | } |
Nathan Scott | e922fff | 2006-03-17 17:27:56 +1100 | [diff] [blame] | 163 | ASSERT(count == be32_to_cpu(btp->count) - be32_to_cpu(btp->stale)); |
| 164 | ASSERT(stale == be32_to_cpu(btp->stale)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | #endif |
| 168 | |
| 169 | /* |
| 170 | * Given a data block and an unused entry from that block, |
| 171 | * return the bestfree entry if any that corresponds to it. |
| 172 | */ |
| 173 | xfs_dir2_data_free_t * |
| 174 | xfs_dir2_data_freefind( |
| 175 | xfs_dir2_data_t *d, /* data block */ |
| 176 | xfs_dir2_data_unused_t *dup) /* data unused entry */ |
| 177 | { |
| 178 | xfs_dir2_data_free_t *dfp; /* bestfree entry */ |
| 179 | xfs_dir2_data_aoff_t off; /* offset value needed */ |
| 180 | #if defined(DEBUG) && defined(__KERNEL__) |
| 181 | int matched; /* matched the value */ |
| 182 | int seenzero; /* saw a 0 bestfree entry */ |
| 183 | #endif |
| 184 | |
| 185 | off = (xfs_dir2_data_aoff_t)((char *)dup - (char *)d); |
| 186 | #if defined(DEBUG) && defined(__KERNEL__) |
| 187 | /* |
| 188 | * Validate some consistency in the bestfree table. |
| 189 | * Check order, non-overlapping entries, and if we find the |
| 190 | * one we're looking for it has to be exact. |
| 191 | */ |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 192 | ASSERT(be32_to_cpu(d->hdr.magic) == XFS_DIR2_DATA_MAGIC || |
| 193 | be32_to_cpu(d->hdr.magic) == XFS_DIR2_BLOCK_MAGIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | for (dfp = &d->hdr.bestfree[0], seenzero = matched = 0; |
| 195 | dfp < &d->hdr.bestfree[XFS_DIR2_DATA_FD_COUNT]; |
| 196 | dfp++) { |
| 197 | if (!dfp->offset) { |
| 198 | ASSERT(!dfp->length); |
| 199 | seenzero = 1; |
| 200 | continue; |
| 201 | } |
| 202 | ASSERT(seenzero == 0); |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 203 | if (be16_to_cpu(dfp->offset) == off) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | matched = 1; |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 205 | ASSERT(dfp->length == dup->length); |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 206 | } else if (off < be16_to_cpu(dfp->offset)) |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 207 | ASSERT(off + be16_to_cpu(dup->length) <= be16_to_cpu(dfp->offset)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | else |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 209 | ASSERT(be16_to_cpu(dfp->offset) + be16_to_cpu(dfp->length) <= off); |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 210 | ASSERT(matched || be16_to_cpu(dfp->length) >= be16_to_cpu(dup->length)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | if (dfp > &d->hdr.bestfree[0]) |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 212 | ASSERT(be16_to_cpu(dfp[-1].length) >= be16_to_cpu(dfp[0].length)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | } |
| 214 | #endif |
| 215 | /* |
| 216 | * If this is smaller than the smallest bestfree entry, |
| 217 | * it can't be there since they're sorted. |
| 218 | */ |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 219 | if (be16_to_cpu(dup->length) < |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 220 | be16_to_cpu(d->hdr.bestfree[XFS_DIR2_DATA_FD_COUNT - 1].length)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | return NULL; |
| 222 | /* |
| 223 | * Look at the three bestfree entries for our guy. |
| 224 | */ |
| 225 | for (dfp = &d->hdr.bestfree[0]; |
| 226 | dfp < &d->hdr.bestfree[XFS_DIR2_DATA_FD_COUNT]; |
| 227 | dfp++) { |
| 228 | if (!dfp->offset) |
| 229 | return NULL; |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 230 | if (be16_to_cpu(dfp->offset) == off) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | return dfp; |
| 232 | } |
| 233 | /* |
| 234 | * Didn't find it. This only happens if there are duplicate lengths. |
| 235 | */ |
| 236 | return NULL; |
| 237 | } |
| 238 | |
| 239 | /* |
| 240 | * Insert an unused-space entry into the bestfree table. |
| 241 | */ |
| 242 | xfs_dir2_data_free_t * /* entry inserted */ |
| 243 | xfs_dir2_data_freeinsert( |
| 244 | xfs_dir2_data_t *d, /* data block pointer */ |
| 245 | xfs_dir2_data_unused_t *dup, /* unused space */ |
| 246 | int *loghead) /* log the data header (out) */ |
| 247 | { |
| 248 | xfs_dir2_data_free_t *dfp; /* bestfree table pointer */ |
| 249 | xfs_dir2_data_free_t new; /* new bestfree entry */ |
| 250 | |
| 251 | #ifdef __KERNEL__ |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 252 | ASSERT(be32_to_cpu(d->hdr.magic) == XFS_DIR2_DATA_MAGIC || |
| 253 | be32_to_cpu(d->hdr.magic) == XFS_DIR2_BLOCK_MAGIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | #endif |
| 255 | dfp = d->hdr.bestfree; |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 256 | new.length = dup->length; |
| 257 | new.offset = cpu_to_be16((char *)dup - (char *)d); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | /* |
| 259 | * Insert at position 0, 1, or 2; or not at all. |
| 260 | */ |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 261 | if (be16_to_cpu(new.length) > be16_to_cpu(dfp[0].length)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | dfp[2] = dfp[1]; |
| 263 | dfp[1] = dfp[0]; |
| 264 | dfp[0] = new; |
| 265 | *loghead = 1; |
| 266 | return &dfp[0]; |
| 267 | } |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 268 | if (be16_to_cpu(new.length) > be16_to_cpu(dfp[1].length)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | dfp[2] = dfp[1]; |
| 270 | dfp[1] = new; |
| 271 | *loghead = 1; |
| 272 | return &dfp[1]; |
| 273 | } |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 274 | if (be16_to_cpu(new.length) > be16_to_cpu(dfp[2].length)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | dfp[2] = new; |
| 276 | *loghead = 1; |
| 277 | return &dfp[2]; |
| 278 | } |
| 279 | return NULL; |
| 280 | } |
| 281 | |
| 282 | /* |
| 283 | * Remove a bestfree entry from the table. |
| 284 | */ |
Christoph Hellwig | ba0f32d | 2005-06-21 15:36:52 +1000 | [diff] [blame] | 285 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | xfs_dir2_data_freeremove( |
| 287 | xfs_dir2_data_t *d, /* data block pointer */ |
| 288 | xfs_dir2_data_free_t *dfp, /* bestfree entry pointer */ |
| 289 | int *loghead) /* out: log data header */ |
| 290 | { |
| 291 | #ifdef __KERNEL__ |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 292 | ASSERT(be32_to_cpu(d->hdr.magic) == XFS_DIR2_DATA_MAGIC || |
| 293 | be32_to_cpu(d->hdr.magic) == XFS_DIR2_BLOCK_MAGIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | #endif |
| 295 | /* |
| 296 | * It's the first entry, slide the next 2 up. |
| 297 | */ |
| 298 | if (dfp == &d->hdr.bestfree[0]) { |
| 299 | d->hdr.bestfree[0] = d->hdr.bestfree[1]; |
| 300 | d->hdr.bestfree[1] = d->hdr.bestfree[2]; |
| 301 | } |
| 302 | /* |
| 303 | * It's the second entry, slide the 3rd entry up. |
| 304 | */ |
| 305 | else if (dfp == &d->hdr.bestfree[1]) |
| 306 | d->hdr.bestfree[1] = d->hdr.bestfree[2]; |
| 307 | /* |
| 308 | * Must be the last entry. |
| 309 | */ |
| 310 | else |
| 311 | ASSERT(dfp == &d->hdr.bestfree[2]); |
| 312 | /* |
| 313 | * Clear the 3rd entry, must be zero now. |
| 314 | */ |
| 315 | d->hdr.bestfree[2].length = 0; |
| 316 | d->hdr.bestfree[2].offset = 0; |
| 317 | *loghead = 1; |
| 318 | } |
| 319 | |
| 320 | /* |
| 321 | * Given a data block, reconstruct its bestfree map. |
| 322 | */ |
| 323 | void |
| 324 | xfs_dir2_data_freescan( |
| 325 | xfs_mount_t *mp, /* filesystem mount point */ |
| 326 | xfs_dir2_data_t *d, /* data block pointer */ |
| 327 | int *loghead, /* out: log data header */ |
| 328 | char *aendp) /* in: caller's endp */ |
| 329 | { |
| 330 | xfs_dir2_block_tail_t *btp; /* block tail */ |
| 331 | xfs_dir2_data_entry_t *dep; /* active data entry */ |
| 332 | xfs_dir2_data_unused_t *dup; /* unused data entry */ |
| 333 | char *endp; /* end of block's data */ |
| 334 | char *p; /* current entry pointer */ |
| 335 | |
| 336 | #ifdef __KERNEL__ |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 337 | ASSERT(be32_to_cpu(d->hdr.magic) == XFS_DIR2_DATA_MAGIC || |
| 338 | be32_to_cpu(d->hdr.magic) == XFS_DIR2_BLOCK_MAGIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | #endif |
| 340 | /* |
| 341 | * Start by clearing the table. |
| 342 | */ |
| 343 | memset(d->hdr.bestfree, 0, sizeof(d->hdr.bestfree)); |
| 344 | *loghead = 1; |
| 345 | /* |
| 346 | * Set up pointers. |
| 347 | */ |
| 348 | p = (char *)d->u; |
| 349 | if (aendp) |
| 350 | endp = aendp; |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 351 | else if (be32_to_cpu(d->hdr.magic) == XFS_DIR2_BLOCK_MAGIC) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | btp = XFS_DIR2_BLOCK_TAIL_P(mp, (xfs_dir2_block_t *)d); |
| 353 | endp = (char *)XFS_DIR2_BLOCK_LEAF_P(btp); |
| 354 | } else |
| 355 | endp = (char *)d + mp->m_dirblksize; |
| 356 | /* |
| 357 | * Loop over the block's entries. |
| 358 | */ |
| 359 | while (p < endp) { |
| 360 | dup = (xfs_dir2_data_unused_t *)p; |
| 361 | /* |
| 362 | * If it's a free entry, insert it. |
| 363 | */ |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 364 | if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | ASSERT((char *)dup - (char *)d == |
Nathan Scott | 1fba9f7 | 2006-03-17 17:27:47 +1100 | [diff] [blame] | 366 | be16_to_cpu(*XFS_DIR2_DATA_UNUSED_TAG_P(dup))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | xfs_dir2_data_freeinsert(d, dup, loghead); |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 368 | p += be16_to_cpu(dup->length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | } |
| 370 | /* |
| 371 | * For active entries, check their tags and skip them. |
| 372 | */ |
| 373 | else { |
| 374 | dep = (xfs_dir2_data_entry_t *)p; |
| 375 | ASSERT((char *)dep - (char *)d == |
Nathan Scott | 3d693c6 | 2006-03-17 17:28:27 +1100 | [diff] [blame] | 376 | be16_to_cpu(*XFS_DIR2_DATA_ENTRY_TAG_P(dep))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | p += XFS_DIR2_DATA_ENTSIZE(dep->namelen); |
| 378 | } |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | /* |
| 383 | * Initialize a data block at the given block number in the directory. |
| 384 | * Give back the buffer for the created block. |
| 385 | */ |
| 386 | int /* error */ |
| 387 | xfs_dir2_data_init( |
| 388 | xfs_da_args_t *args, /* directory operation args */ |
| 389 | xfs_dir2_db_t blkno, /* logical dir block number */ |
| 390 | xfs_dabuf_t **bpp) /* output block buffer */ |
| 391 | { |
| 392 | xfs_dabuf_t *bp; /* block buffer */ |
| 393 | xfs_dir2_data_t *d; /* pointer to block */ |
| 394 | xfs_inode_t *dp; /* incore directory inode */ |
| 395 | xfs_dir2_data_unused_t *dup; /* unused entry pointer */ |
| 396 | int error; /* error return value */ |
| 397 | int i; /* bestfree index */ |
| 398 | xfs_mount_t *mp; /* filesystem mount point */ |
| 399 | xfs_trans_t *tp; /* transaction pointer */ |
| 400 | int t; /* temp */ |
| 401 | |
| 402 | dp = args->dp; |
| 403 | mp = dp->i_mount; |
| 404 | tp = args->trans; |
| 405 | /* |
| 406 | * Get the buffer set up for the block. |
| 407 | */ |
| 408 | error = xfs_da_get_buf(tp, dp, XFS_DIR2_DB_TO_DA(mp, blkno), -1, &bp, |
| 409 | XFS_DATA_FORK); |
| 410 | if (error) { |
| 411 | return error; |
| 412 | } |
| 413 | ASSERT(bp != NULL); |
| 414 | /* |
| 415 | * Initialize the header. |
| 416 | */ |
| 417 | d = bp->data; |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 418 | d->hdr.magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC); |
| 419 | d->hdr.bestfree[0].offset = cpu_to_be16(sizeof(d->hdr)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | for (i = 1; i < XFS_DIR2_DATA_FD_COUNT; i++) { |
| 421 | d->hdr.bestfree[i].length = 0; |
| 422 | d->hdr.bestfree[i].offset = 0; |
| 423 | } |
| 424 | /* |
| 425 | * Set up an unused entry for the block's body. |
| 426 | */ |
| 427 | dup = &d->u[0].unused; |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 428 | dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | |
| 430 | t=mp->m_dirblksize - (uint)sizeof(d->hdr); |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 431 | d->hdr.bestfree[0].length = cpu_to_be16(t); |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 432 | dup->length = cpu_to_be16(t); |
Nathan Scott | 1fba9f7 | 2006-03-17 17:27:47 +1100 | [diff] [blame] | 433 | *XFS_DIR2_DATA_UNUSED_TAG_P(dup) = cpu_to_be16((char *)dup - (char *)d); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | /* |
| 435 | * Log it and return it. |
| 436 | */ |
| 437 | xfs_dir2_data_log_header(tp, bp); |
| 438 | xfs_dir2_data_log_unused(tp, bp, dup); |
| 439 | *bpp = bp; |
| 440 | return 0; |
| 441 | } |
| 442 | |
| 443 | /* |
| 444 | * Log an active data entry from the block. |
| 445 | */ |
| 446 | void |
| 447 | xfs_dir2_data_log_entry( |
| 448 | xfs_trans_t *tp, /* transaction pointer */ |
| 449 | xfs_dabuf_t *bp, /* block buffer */ |
| 450 | xfs_dir2_data_entry_t *dep) /* data entry pointer */ |
| 451 | { |
| 452 | xfs_dir2_data_t *d; /* data block pointer */ |
| 453 | |
| 454 | d = bp->data; |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 455 | ASSERT(be32_to_cpu(d->hdr.magic) == XFS_DIR2_DATA_MAGIC || |
| 456 | be32_to_cpu(d->hdr.magic) == XFS_DIR2_BLOCK_MAGIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | xfs_da_log_buf(tp, bp, (uint)((char *)dep - (char *)d), |
| 458 | (uint)((char *)(XFS_DIR2_DATA_ENTRY_TAG_P(dep) + 1) - |
| 459 | (char *)d - 1)); |
| 460 | } |
| 461 | |
| 462 | /* |
| 463 | * Log a data block header. |
| 464 | */ |
| 465 | void |
| 466 | xfs_dir2_data_log_header( |
| 467 | xfs_trans_t *tp, /* transaction pointer */ |
| 468 | xfs_dabuf_t *bp) /* block buffer */ |
| 469 | { |
| 470 | xfs_dir2_data_t *d; /* data block pointer */ |
| 471 | |
| 472 | d = bp->data; |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 473 | ASSERT(be32_to_cpu(d->hdr.magic) == XFS_DIR2_DATA_MAGIC || |
| 474 | be32_to_cpu(d->hdr.magic) == XFS_DIR2_BLOCK_MAGIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | xfs_da_log_buf(tp, bp, (uint)((char *)&d->hdr - (char *)d), |
| 476 | (uint)(sizeof(d->hdr) - 1)); |
| 477 | } |
| 478 | |
| 479 | /* |
| 480 | * Log a data unused entry. |
| 481 | */ |
| 482 | void |
| 483 | xfs_dir2_data_log_unused( |
| 484 | xfs_trans_t *tp, /* transaction pointer */ |
| 485 | xfs_dabuf_t *bp, /* block buffer */ |
| 486 | xfs_dir2_data_unused_t *dup) /* data unused pointer */ |
| 487 | { |
| 488 | xfs_dir2_data_t *d; /* data block pointer */ |
| 489 | |
| 490 | d = bp->data; |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 491 | ASSERT(be32_to_cpu(d->hdr.magic) == XFS_DIR2_DATA_MAGIC || |
| 492 | be32_to_cpu(d->hdr.magic) == XFS_DIR2_BLOCK_MAGIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | /* |
| 494 | * Log the first part of the unused entry. |
| 495 | */ |
| 496 | xfs_da_log_buf(tp, bp, (uint)((char *)dup - (char *)d), |
| 497 | (uint)((char *)&dup->length + sizeof(dup->length) - |
| 498 | 1 - (char *)d)); |
| 499 | /* |
| 500 | * Log the end (tag) of the unused entry. |
| 501 | */ |
| 502 | xfs_da_log_buf(tp, bp, |
| 503 | (uint)((char *)XFS_DIR2_DATA_UNUSED_TAG_P(dup) - (char *)d), |
| 504 | (uint)((char *)XFS_DIR2_DATA_UNUSED_TAG_P(dup) - (char *)d + |
| 505 | sizeof(xfs_dir2_data_off_t) - 1)); |
| 506 | } |
| 507 | |
| 508 | /* |
| 509 | * Make a byte range in the data block unused. |
| 510 | * Its current contents are unimportant. |
| 511 | */ |
| 512 | void |
| 513 | xfs_dir2_data_make_free( |
| 514 | xfs_trans_t *tp, /* transaction pointer */ |
| 515 | xfs_dabuf_t *bp, /* block buffer */ |
| 516 | xfs_dir2_data_aoff_t offset, /* starting byte offset */ |
| 517 | xfs_dir2_data_aoff_t len, /* length in bytes */ |
| 518 | int *needlogp, /* out: log header */ |
| 519 | int *needscanp) /* out: regen bestfree */ |
| 520 | { |
| 521 | xfs_dir2_data_t *d; /* data block pointer */ |
| 522 | xfs_dir2_data_free_t *dfp; /* bestfree pointer */ |
| 523 | char *endptr; /* end of data area */ |
| 524 | xfs_mount_t *mp; /* filesystem mount point */ |
| 525 | int needscan; /* need to regen bestfree */ |
| 526 | xfs_dir2_data_unused_t *newdup; /* new unused entry */ |
| 527 | xfs_dir2_data_unused_t *postdup; /* unused entry after us */ |
| 528 | xfs_dir2_data_unused_t *prevdup; /* unused entry before us */ |
| 529 | |
| 530 | mp = tp->t_mountp; |
| 531 | d = bp->data; |
| 532 | /* |
| 533 | * Figure out where the end of the data area is. |
| 534 | */ |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 535 | if (be32_to_cpu(d->hdr.magic) == XFS_DIR2_DATA_MAGIC) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | endptr = (char *)d + mp->m_dirblksize; |
| 537 | else { |
| 538 | xfs_dir2_block_tail_t *btp; /* block tail */ |
| 539 | |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 540 | ASSERT(be32_to_cpu(d->hdr.magic) == XFS_DIR2_BLOCK_MAGIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | btp = XFS_DIR2_BLOCK_TAIL_P(mp, (xfs_dir2_block_t *)d); |
| 542 | endptr = (char *)XFS_DIR2_BLOCK_LEAF_P(btp); |
| 543 | } |
| 544 | /* |
| 545 | * If this isn't the start of the block, then back up to |
| 546 | * the previous entry and see if it's free. |
| 547 | */ |
| 548 | if (offset > sizeof(d->hdr)) { |
Nathan Scott | 3d693c6 | 2006-03-17 17:28:27 +1100 | [diff] [blame] | 549 | __be16 *tagp; /* tag just before us */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | |
Nathan Scott | 3d693c6 | 2006-03-17 17:28:27 +1100 | [diff] [blame] | 551 | tagp = (__be16 *)((char *)d + offset) - 1; |
| 552 | prevdup = (xfs_dir2_data_unused_t *)((char *)d + be16_to_cpu(*tagp)); |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 553 | if (be16_to_cpu(prevdup->freetag) != XFS_DIR2_DATA_FREE_TAG) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | prevdup = NULL; |
| 555 | } else |
| 556 | prevdup = NULL; |
| 557 | /* |
| 558 | * If this isn't the end of the block, see if the entry after |
| 559 | * us is free. |
| 560 | */ |
| 561 | if ((char *)d + offset + len < endptr) { |
| 562 | postdup = |
| 563 | (xfs_dir2_data_unused_t *)((char *)d + offset + len); |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 564 | if (be16_to_cpu(postdup->freetag) != XFS_DIR2_DATA_FREE_TAG) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | postdup = NULL; |
| 566 | } else |
| 567 | postdup = NULL; |
| 568 | ASSERT(*needscanp == 0); |
| 569 | needscan = 0; |
| 570 | /* |
| 571 | * Previous and following entries are both free, |
| 572 | * merge everything into a single free entry. |
| 573 | */ |
| 574 | if (prevdup && postdup) { |
| 575 | xfs_dir2_data_free_t *dfp2; /* another bestfree pointer */ |
| 576 | |
| 577 | /* |
| 578 | * See if prevdup and/or postdup are in bestfree table. |
| 579 | */ |
| 580 | dfp = xfs_dir2_data_freefind(d, prevdup); |
| 581 | dfp2 = xfs_dir2_data_freefind(d, postdup); |
| 582 | /* |
| 583 | * We need a rescan unless there are exactly 2 free entries |
| 584 | * namely our two. Then we know what's happening, otherwise |
| 585 | * since the third bestfree is there, there might be more |
| 586 | * entries. |
| 587 | */ |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 588 | needscan = (d->hdr.bestfree[2].length != 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | /* |
| 590 | * Fix up the new big freespace. |
| 591 | */ |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 592 | be16_add(&prevdup->length, len + be16_to_cpu(postdup->length)); |
Nathan Scott | 1fba9f7 | 2006-03-17 17:27:47 +1100 | [diff] [blame] | 593 | *XFS_DIR2_DATA_UNUSED_TAG_P(prevdup) = |
| 594 | cpu_to_be16((char *)prevdup - (char *)d); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | xfs_dir2_data_log_unused(tp, bp, prevdup); |
| 596 | if (!needscan) { |
| 597 | /* |
| 598 | * Has to be the case that entries 0 and 1 are |
| 599 | * dfp and dfp2 (don't know which is which), and |
| 600 | * entry 2 is empty. |
| 601 | * Remove entry 1 first then entry 0. |
| 602 | */ |
| 603 | ASSERT(dfp && dfp2); |
| 604 | if (dfp == &d->hdr.bestfree[1]) { |
| 605 | dfp = &d->hdr.bestfree[0]; |
| 606 | ASSERT(dfp2 == dfp); |
| 607 | dfp2 = &d->hdr.bestfree[1]; |
| 608 | } |
| 609 | xfs_dir2_data_freeremove(d, dfp2, needlogp); |
| 610 | xfs_dir2_data_freeremove(d, dfp, needlogp); |
| 611 | /* |
| 612 | * Now insert the new entry. |
| 613 | */ |
| 614 | dfp = xfs_dir2_data_freeinsert(d, prevdup, needlogp); |
| 615 | ASSERT(dfp == &d->hdr.bestfree[0]); |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 616 | ASSERT(dfp->length == prevdup->length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | ASSERT(!dfp[1].length); |
| 618 | ASSERT(!dfp[2].length); |
| 619 | } |
| 620 | } |
| 621 | /* |
| 622 | * The entry before us is free, merge with it. |
| 623 | */ |
| 624 | else if (prevdup) { |
| 625 | dfp = xfs_dir2_data_freefind(d, prevdup); |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 626 | be16_add(&prevdup->length, len); |
Nathan Scott | 1fba9f7 | 2006-03-17 17:27:47 +1100 | [diff] [blame] | 627 | *XFS_DIR2_DATA_UNUSED_TAG_P(prevdup) = |
| 628 | cpu_to_be16((char *)prevdup - (char *)d); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | xfs_dir2_data_log_unused(tp, bp, prevdup); |
| 630 | /* |
| 631 | * If the previous entry was in the table, the new entry |
| 632 | * is longer, so it will be in the table too. Remove |
| 633 | * the old one and add the new one. |
| 634 | */ |
| 635 | if (dfp) { |
| 636 | xfs_dir2_data_freeremove(d, dfp, needlogp); |
| 637 | (void)xfs_dir2_data_freeinsert(d, prevdup, needlogp); |
| 638 | } |
| 639 | /* |
| 640 | * Otherwise we need a scan if the new entry is big enough. |
| 641 | */ |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 642 | else { |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 643 | needscan = be16_to_cpu(prevdup->length) > |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 644 | be16_to_cpu(d->hdr.bestfree[2].length); |
| 645 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | } |
| 647 | /* |
| 648 | * The following entry is free, merge with it. |
| 649 | */ |
| 650 | else if (postdup) { |
| 651 | dfp = xfs_dir2_data_freefind(d, postdup); |
| 652 | newdup = (xfs_dir2_data_unused_t *)((char *)d + offset); |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 653 | newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG); |
| 654 | newdup->length = cpu_to_be16(len + be16_to_cpu(postdup->length)); |
Nathan Scott | 1fba9f7 | 2006-03-17 17:27:47 +1100 | [diff] [blame] | 655 | *XFS_DIR2_DATA_UNUSED_TAG_P(newdup) = |
| 656 | cpu_to_be16((char *)newdup - (char *)d); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | xfs_dir2_data_log_unused(tp, bp, newdup); |
| 658 | /* |
| 659 | * If the following entry was in the table, the new entry |
| 660 | * is longer, so it will be in the table too. Remove |
| 661 | * the old one and add the new one. |
| 662 | */ |
| 663 | if (dfp) { |
| 664 | xfs_dir2_data_freeremove(d, dfp, needlogp); |
| 665 | (void)xfs_dir2_data_freeinsert(d, newdup, needlogp); |
| 666 | } |
| 667 | /* |
| 668 | * Otherwise we need a scan if the new entry is big enough. |
| 669 | */ |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 670 | else { |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 671 | needscan = be16_to_cpu(newdup->length) > |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 672 | be16_to_cpu(d->hdr.bestfree[2].length); |
| 673 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | } |
| 675 | /* |
| 676 | * Neither neighbor is free. Make a new entry. |
| 677 | */ |
| 678 | else { |
| 679 | newdup = (xfs_dir2_data_unused_t *)((char *)d + offset); |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 680 | newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG); |
| 681 | newdup->length = cpu_to_be16(len); |
Nathan Scott | 1fba9f7 | 2006-03-17 17:27:47 +1100 | [diff] [blame] | 682 | *XFS_DIR2_DATA_UNUSED_TAG_P(newdup) = |
| 683 | cpu_to_be16((char *)newdup - (char *)d); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | xfs_dir2_data_log_unused(tp, bp, newdup); |
| 685 | (void)xfs_dir2_data_freeinsert(d, newdup, needlogp); |
| 686 | } |
| 687 | *needscanp = needscan; |
| 688 | } |
| 689 | |
| 690 | /* |
| 691 | * Take a byte range out of an existing unused space and make it un-free. |
| 692 | */ |
| 693 | void |
| 694 | xfs_dir2_data_use_free( |
| 695 | xfs_trans_t *tp, /* transaction pointer */ |
| 696 | xfs_dabuf_t *bp, /* data block buffer */ |
| 697 | xfs_dir2_data_unused_t *dup, /* unused entry */ |
| 698 | xfs_dir2_data_aoff_t offset, /* starting offset to use */ |
| 699 | xfs_dir2_data_aoff_t len, /* length to use */ |
| 700 | int *needlogp, /* out: need to log header */ |
| 701 | int *needscanp) /* out: need regen bestfree */ |
| 702 | { |
| 703 | xfs_dir2_data_t *d; /* data block */ |
| 704 | xfs_dir2_data_free_t *dfp; /* bestfree pointer */ |
| 705 | int matchback; /* matches end of freespace */ |
| 706 | int matchfront; /* matches start of freespace */ |
| 707 | int needscan; /* need to regen bestfree */ |
| 708 | xfs_dir2_data_unused_t *newdup; /* new unused entry */ |
| 709 | xfs_dir2_data_unused_t *newdup2; /* another new unused entry */ |
| 710 | int oldlen; /* old unused entry's length */ |
| 711 | |
| 712 | d = bp->data; |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 713 | ASSERT(be32_to_cpu(d->hdr.magic) == XFS_DIR2_DATA_MAGIC || |
| 714 | be32_to_cpu(d->hdr.magic) == XFS_DIR2_BLOCK_MAGIC); |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 715 | ASSERT(be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | ASSERT(offset >= (char *)dup - (char *)d); |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 717 | ASSERT(offset + len <= (char *)dup + be16_to_cpu(dup->length) - (char *)d); |
Nathan Scott | 1fba9f7 | 2006-03-17 17:27:47 +1100 | [diff] [blame] | 718 | ASSERT((char *)dup - (char *)d == be16_to_cpu(*XFS_DIR2_DATA_UNUSED_TAG_P(dup))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | /* |
| 720 | * Look up the entry in the bestfree table. |
| 721 | */ |
| 722 | dfp = xfs_dir2_data_freefind(d, dup); |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 723 | oldlen = be16_to_cpu(dup->length); |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 724 | ASSERT(dfp || oldlen <= be16_to_cpu(d->hdr.bestfree[2].length)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | /* |
| 726 | * Check for alignment with front and back of the entry. |
| 727 | */ |
| 728 | matchfront = (char *)dup - (char *)d == offset; |
| 729 | matchback = (char *)dup + oldlen - (char *)d == offset + len; |
| 730 | ASSERT(*needscanp == 0); |
| 731 | needscan = 0; |
| 732 | /* |
| 733 | * If we matched it exactly we just need to get rid of it from |
| 734 | * the bestfree table. |
| 735 | */ |
| 736 | if (matchfront && matchback) { |
| 737 | if (dfp) { |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 738 | needscan = (d->hdr.bestfree[2].offset != 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | if (!needscan) |
| 740 | xfs_dir2_data_freeremove(d, dfp, needlogp); |
| 741 | } |
| 742 | } |
| 743 | /* |
| 744 | * We match the first part of the entry. |
| 745 | * Make a new entry with the remaining freespace. |
| 746 | */ |
| 747 | else if (matchfront) { |
| 748 | newdup = (xfs_dir2_data_unused_t *)((char *)d + offset + len); |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 749 | newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG); |
| 750 | newdup->length = cpu_to_be16(oldlen - len); |
Nathan Scott | 1fba9f7 | 2006-03-17 17:27:47 +1100 | [diff] [blame] | 751 | *XFS_DIR2_DATA_UNUSED_TAG_P(newdup) = |
| 752 | cpu_to_be16((char *)newdup - (char *)d); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | xfs_dir2_data_log_unused(tp, bp, newdup); |
| 754 | /* |
| 755 | * If it was in the table, remove it and add the new one. |
| 756 | */ |
| 757 | if (dfp) { |
| 758 | xfs_dir2_data_freeremove(d, dfp, needlogp); |
| 759 | dfp = xfs_dir2_data_freeinsert(d, newdup, needlogp); |
| 760 | ASSERT(dfp != NULL); |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 761 | ASSERT(dfp->length == newdup->length); |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 762 | ASSERT(be16_to_cpu(dfp->offset) == (char *)newdup - (char *)d); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | /* |
| 764 | * If we got inserted at the last slot, |
| 765 | * that means we don't know if there was a better |
| 766 | * choice for the last slot, or not. Rescan. |
| 767 | */ |
| 768 | needscan = dfp == &d->hdr.bestfree[2]; |
| 769 | } |
| 770 | } |
| 771 | /* |
| 772 | * We match the last part of the entry. |
| 773 | * Trim the allocated space off the tail of the entry. |
| 774 | */ |
| 775 | else if (matchback) { |
| 776 | newdup = dup; |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 777 | newdup->length = cpu_to_be16(((char *)d + offset) - (char *)newdup); |
Nathan Scott | 1fba9f7 | 2006-03-17 17:27:47 +1100 | [diff] [blame] | 778 | *XFS_DIR2_DATA_UNUSED_TAG_P(newdup) = |
| 779 | cpu_to_be16((char *)newdup - (char *)d); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | xfs_dir2_data_log_unused(tp, bp, newdup); |
| 781 | /* |
| 782 | * If it was in the table, remove it and add the new one. |
| 783 | */ |
| 784 | if (dfp) { |
| 785 | xfs_dir2_data_freeremove(d, dfp, needlogp); |
| 786 | dfp = xfs_dir2_data_freeinsert(d, newdup, needlogp); |
| 787 | ASSERT(dfp != NULL); |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 788 | ASSERT(dfp->length == newdup->length); |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 789 | ASSERT(be16_to_cpu(dfp->offset) == (char *)newdup - (char *)d); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | /* |
| 791 | * If we got inserted at the last slot, |
| 792 | * that means we don't know if there was a better |
| 793 | * choice for the last slot, or not. Rescan. |
| 794 | */ |
| 795 | needscan = dfp == &d->hdr.bestfree[2]; |
| 796 | } |
| 797 | } |
| 798 | /* |
| 799 | * Poking out the middle of an entry. |
| 800 | * Make two new entries. |
| 801 | */ |
| 802 | else { |
| 803 | newdup = dup; |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 804 | newdup->length = cpu_to_be16(((char *)d + offset) - (char *)newdup); |
Nathan Scott | 1fba9f7 | 2006-03-17 17:27:47 +1100 | [diff] [blame] | 805 | *XFS_DIR2_DATA_UNUSED_TAG_P(newdup) = |
| 806 | cpu_to_be16((char *)newdup - (char *)d); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | xfs_dir2_data_log_unused(tp, bp, newdup); |
| 808 | newdup2 = (xfs_dir2_data_unused_t *)((char *)d + offset + len); |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 809 | newdup2->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG); |
| 810 | newdup2->length = cpu_to_be16(oldlen - len - be16_to_cpu(newdup->length)); |
Nathan Scott | 1fba9f7 | 2006-03-17 17:27:47 +1100 | [diff] [blame] | 811 | *XFS_DIR2_DATA_UNUSED_TAG_P(newdup2) = |
| 812 | cpu_to_be16((char *)newdup2 - (char *)d); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | xfs_dir2_data_log_unused(tp, bp, newdup2); |
| 814 | /* |
| 815 | * If the old entry was in the table, we need to scan |
| 816 | * if the 3rd entry was valid, since these entries |
| 817 | * are smaller than the old one. |
| 818 | * If we don't need to scan that means there were 1 or 2 |
| 819 | * entries in the table, and removing the old and adding |
| 820 | * the 2 new will work. |
| 821 | */ |
| 822 | if (dfp) { |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 823 | needscan = (d->hdr.bestfree[2].length != 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | if (!needscan) { |
| 825 | xfs_dir2_data_freeremove(d, dfp, needlogp); |
| 826 | (void)xfs_dir2_data_freeinsert(d, newdup, |
| 827 | needlogp); |
| 828 | (void)xfs_dir2_data_freeinsert(d, newdup2, |
| 829 | needlogp); |
| 830 | } |
| 831 | } |
| 832 | } |
| 833 | *needscanp = needscan; |
| 834 | } |