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