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