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