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