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. |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 3 | * Copyright (c) 2013 Red Hat, Inc. |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 4 | * All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License as |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * published by the Free Software Foundation. |
| 9 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 10 | * This program is distributed in the hope that it would be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write the Free Software Foundation, |
| 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include "xfs.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 20 | #include "xfs_fs.h" |
Dave Chinner | a4fbe6a | 2013-10-23 10:51:50 +1100 | [diff] [blame] | 21 | #include "xfs_format.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 22 | #include "xfs_log_format.h" |
| 23 | #include "xfs_trans_resv.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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_mount.h" |
Dave Chinner | 5706278 | 2013-10-15 09:17:51 +1100 | [diff] [blame] | 27 | #include "xfs_da_format.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_inode.h" |
Dave Chinner | 2b9ab5a | 2013-08-12 20:49:37 +1000 | [diff] [blame] | 30 | #include "xfs_dir2.h" |
Christoph Hellwig | 5792664 | 2011-07-13 13:43:48 +0200 | [diff] [blame] | 31 | #include "xfs_dir2_priv.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include "xfs_error.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 33 | #include "xfs_trans.h" |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame] | 34 | #include "xfs_buf_item.h" |
| 35 | #include "xfs_cksum.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame] | 43 | __xfs_dir3_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; |
Dave Chinner | 9d23fc8 | 2013-10-29 22:11:48 +1100 | [diff] [blame^] | 65 | const struct xfs_dir_ops *ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | |
Dave Chinner | 82025d7 | 2012-11-12 22:54:12 +1100 | [diff] [blame] | 67 | mp = bp->b_target->bt_mount; |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 68 | hdr = bp->b_addr; |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 69 | bf = xfs_dir3_data_bestfree_p(hdr); |
| 70 | p = (char *)xfs_dir3_data_entry_p(hdr); |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 71 | |
Dave Chinner | 9d23fc8 | 2013-10-29 22:11:48 +1100 | [diff] [blame^] | 72 | /* |
| 73 | * We can be passed a null dp here from a verifier, so manually |
| 74 | * configure the ops here in that case. |
| 75 | */ |
| 76 | if (dp) |
| 77 | ops = dp->d_ops; |
| 78 | else if (xfs_sb_version_hascrc(&mp->m_sb)) |
| 79 | ops = &xfs_dir3_ops; |
| 80 | else if (xfs_sb_version_hasftype(&mp->m_sb)) |
| 81 | ops = &xfs_dir2_ftype_ops; |
| 82 | else |
| 83 | ops = &xfs_dir2_ops; |
| 84 | |
Dave Chinner | 82025d7 | 2012-11-12 22:54:12 +1100 | [diff] [blame] | 85 | switch (hdr->magic) { |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 86 | case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC): |
Dave Chinner | 82025d7 | 2012-11-12 22:54:12 +1100 | [diff] [blame] | 87 | case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC): |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 88 | btp = xfs_dir2_block_tail_p(mp, hdr); |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 89 | lep = xfs_dir2_block_leaf_p(btp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | endp = (char *)lep; |
Dave Chinner | 82025d7 | 2012-11-12 22:54:12 +1100 | [diff] [blame] | 91 | break; |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame] | 92 | case cpu_to_be32(XFS_DIR3_DATA_MAGIC): |
Dave Chinner | 82025d7 | 2012-11-12 22:54:12 +1100 | [diff] [blame] | 93 | case cpu_to_be32(XFS_DIR2_DATA_MAGIC): |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 94 | endp = (char *)hdr + mp->m_dirblksize; |
Dave Chinner | 82025d7 | 2012-11-12 22:54:12 +1100 | [diff] [blame] | 95 | break; |
| 96 | default: |
| 97 | XFS_ERROR_REPORT("Bad Magic", XFS_ERRLEVEL_LOW, mp); |
| 98 | return EFSCORRUPTED; |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 99 | } |
| 100 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | count = lastfree = freeseen = 0; |
| 102 | /* |
| 103 | * Account for zero bestfree entries. |
| 104 | */ |
| 105 | if (!bf[0].length) { |
Dave Chinner | 82025d7 | 2012-11-12 22:54:12 +1100 | [diff] [blame] | 106 | XFS_WANT_CORRUPTED_RETURN(!bf[0].offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | freeseen |= 1 << 0; |
| 108 | } |
| 109 | if (!bf[1].length) { |
Dave Chinner | 82025d7 | 2012-11-12 22:54:12 +1100 | [diff] [blame] | 110 | XFS_WANT_CORRUPTED_RETURN(!bf[1].offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | freeseen |= 1 << 1; |
| 112 | } |
| 113 | if (!bf[2].length) { |
Dave Chinner | 82025d7 | 2012-11-12 22:54:12 +1100 | [diff] [blame] | 114 | XFS_WANT_CORRUPTED_RETURN(!bf[2].offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | freeseen |= 1 << 2; |
| 116 | } |
Dave Chinner | 82025d7 | 2012-11-12 22:54:12 +1100 | [diff] [blame] | 117 | |
| 118 | XFS_WANT_CORRUPTED_RETURN(be16_to_cpu(bf[0].length) >= |
| 119 | be16_to_cpu(bf[1].length)); |
| 120 | XFS_WANT_CORRUPTED_RETURN(be16_to_cpu(bf[1].length) >= |
| 121 | be16_to_cpu(bf[2].length)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | /* |
| 123 | * Loop over the data/unused entries. |
| 124 | */ |
| 125 | while (p < endp) { |
| 126 | dup = (xfs_dir2_data_unused_t *)p; |
| 127 | /* |
| 128 | * If it's unused, look for the space in the bestfree table. |
| 129 | * If we find it, account for that, else make sure it |
| 130 | * doesn't need to be there. |
| 131 | */ |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 132 | if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) { |
Dave Chinner | 82025d7 | 2012-11-12 22:54:12 +1100 | [diff] [blame] | 133 | XFS_WANT_CORRUPTED_RETURN(lastfree == 0); |
| 134 | XFS_WANT_CORRUPTED_RETURN( |
| 135 | be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)) == |
| 136 | (char *)dup - (char *)hdr); |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 137 | dfp = xfs_dir2_data_freefind(hdr, dup); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | if (dfp) { |
| 139 | i = (int)(dfp - bf); |
Dave Chinner | 82025d7 | 2012-11-12 22:54:12 +1100 | [diff] [blame] | 140 | XFS_WANT_CORRUPTED_RETURN( |
| 141 | (freeseen & (1 << i)) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | freeseen |= 1 << i; |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 143 | } else { |
Dave Chinner | 82025d7 | 2012-11-12 22:54:12 +1100 | [diff] [blame] | 144 | XFS_WANT_CORRUPTED_RETURN( |
| 145 | be16_to_cpu(dup->length) <= |
| 146 | be16_to_cpu(bf[2].length)); |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 147 | } |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 148 | p += be16_to_cpu(dup->length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | lastfree = 1; |
| 150 | continue; |
| 151 | } |
| 152 | /* |
| 153 | * It's a real entry. Validate the fields. |
| 154 | * If this is a block directory then make sure it's |
| 155 | * in the leaf section of the block. |
| 156 | * The linear search is crude but this is DEBUG code. |
| 157 | */ |
| 158 | dep = (xfs_dir2_data_entry_t *)p; |
Dave Chinner | 82025d7 | 2012-11-12 22:54:12 +1100 | [diff] [blame] | 159 | XFS_WANT_CORRUPTED_RETURN(dep->namelen != 0); |
| 160 | XFS_WANT_CORRUPTED_RETURN( |
| 161 | !xfs_dir_ino_validate(mp, be64_to_cpu(dep->inumber))); |
| 162 | XFS_WANT_CORRUPTED_RETURN( |
Dave Chinner | 9d23fc8 | 2013-10-29 22:11:48 +1100 | [diff] [blame^] | 163 | be16_to_cpu(*ops->data_entry_tag_p(dep)) == |
Dave Chinner | 82025d7 | 2012-11-12 22:54:12 +1100 | [diff] [blame] | 164 | (char *)dep - (char *)hdr); |
Dave Chinner | 1c55cec | 2013-08-12 20:50:10 +1000 | [diff] [blame] | 165 | XFS_WANT_CORRUPTED_RETURN( |
Dave Chinner | 9d23fc8 | 2013-10-29 22:11:48 +1100 | [diff] [blame^] | 166 | ops->data_get_ftype(dep) < XFS_DIR3_FT_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | count++; |
| 168 | lastfree = 0; |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 169 | if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) || |
| 170 | hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) { |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 171 | addr = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | (xfs_dir2_data_aoff_t) |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 173 | ((char *)dep - (char *)hdr)); |
Barry Naujok | 5163f95 | 2008-05-21 16:41:01 +1000 | [diff] [blame] | 174 | name.name = dep->name; |
| 175 | name.len = dep->namelen; |
| 176 | hash = mp->m_dirnameops->hashname(&name); |
Nathan Scott | e922fff | 2006-03-17 17:27:56 +1100 | [diff] [blame] | 177 | for (i = 0; i < be32_to_cpu(btp->count); i++) { |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 178 | if (be32_to_cpu(lep[i].address) == addr && |
| 179 | be32_to_cpu(lep[i].hashval) == hash) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | break; |
| 181 | } |
Dave Chinner | 82025d7 | 2012-11-12 22:54:12 +1100 | [diff] [blame] | 182 | XFS_WANT_CORRUPTED_RETURN(i < be32_to_cpu(btp->count)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | } |
Dave Chinner | 9d23fc8 | 2013-10-29 22:11:48 +1100 | [diff] [blame^] | 184 | p += ops->data_entsize(dep->namelen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | } |
| 186 | /* |
| 187 | * Need to have seen all the entries and all the bestfree slots. |
| 188 | */ |
Dave Chinner | 82025d7 | 2012-11-12 22:54:12 +1100 | [diff] [blame] | 189 | XFS_WANT_CORRUPTED_RETURN(freeseen == 7); |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 190 | if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) || |
| 191 | hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) { |
Nathan Scott | e922fff | 2006-03-17 17:27:56 +1100 | [diff] [blame] | 192 | for (i = stale = 0; i < be32_to_cpu(btp->count); i++) { |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 193 | if (lep[i].address == |
| 194 | cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | stale++; |
| 196 | if (i > 0) |
Dave Chinner | 82025d7 | 2012-11-12 22:54:12 +1100 | [diff] [blame] | 197 | XFS_WANT_CORRUPTED_RETURN( |
| 198 | be32_to_cpu(lep[i].hashval) >= |
| 199 | be32_to_cpu(lep[i - 1].hashval)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | } |
Dave Chinner | 82025d7 | 2012-11-12 22:54:12 +1100 | [diff] [blame] | 201 | XFS_WANT_CORRUPTED_RETURN(count == |
| 202 | be32_to_cpu(btp->count) - be32_to_cpu(btp->stale)); |
| 203 | XFS_WANT_CORRUPTED_RETURN(stale == be32_to_cpu(btp->stale)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | } |
Dave Chinner | 82025d7 | 2012-11-12 22:54:12 +1100 | [diff] [blame] | 205 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame] | 208 | static bool |
| 209 | xfs_dir3_data_verify( |
Dave Chinner | e481357 | 2012-11-12 22:54:14 +1100 | [diff] [blame] | 210 | struct xfs_buf *bp) |
| 211 | { |
| 212 | struct xfs_mount *mp = bp->b_target->bt_mount; |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame] | 213 | struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr; |
Dave Chinner | e481357 | 2012-11-12 22:54:14 +1100 | [diff] [blame] | 214 | |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame] | 215 | if (xfs_sb_version_hascrc(&mp->m_sb)) { |
| 216 | if (hdr3->magic != cpu_to_be32(XFS_DIR3_DATA_MAGIC)) |
| 217 | return false; |
| 218 | if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_uuid)) |
| 219 | return false; |
| 220 | if (be64_to_cpu(hdr3->blkno) != bp->b_bn) |
| 221 | return false; |
| 222 | } else { |
| 223 | if (hdr3->magic != cpu_to_be32(XFS_DIR2_DATA_MAGIC)) |
| 224 | return false; |
Dave Chinner | e481357 | 2012-11-12 22:54:14 +1100 | [diff] [blame] | 225 | } |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame] | 226 | if (__xfs_dir3_data_check(NULL, bp)) |
| 227 | return false; |
| 228 | return true; |
Dave Chinner | 612cfbf | 2012-11-14 17:52:32 +1100 | [diff] [blame] | 229 | } |
Dave Chinner | e481357 | 2012-11-12 22:54:14 +1100 | [diff] [blame] | 230 | |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 231 | /* |
| 232 | * Readahead of the first block of the directory when it is opened is completely |
| 233 | * oblivious to the format of the directory. Hence we can either get a block |
| 234 | * format buffer or a data format buffer on readahead. |
| 235 | */ |
| 236 | static void |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame] | 237 | xfs_dir3_data_reada_verify( |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 238 | struct xfs_buf *bp) |
Dave Chinner | 612cfbf | 2012-11-14 17:52:32 +1100 | [diff] [blame] | 239 | { |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 240 | struct xfs_mount *mp = bp->b_target->bt_mount; |
| 241 | struct xfs_dir2_data_hdr *hdr = bp->b_addr; |
| 242 | |
| 243 | switch (hdr->magic) { |
| 244 | case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC): |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 245 | case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC): |
| 246 | bp->b_ops = &xfs_dir3_block_buf_ops; |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 247 | bp->b_ops->verify_read(bp); |
| 248 | return; |
| 249 | case cpu_to_be32(XFS_DIR2_DATA_MAGIC): |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame] | 250 | case cpu_to_be32(XFS_DIR3_DATA_MAGIC): |
| 251 | xfs_dir3_data_verify(bp); |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 252 | return; |
| 253 | default: |
| 254 | XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, hdr); |
| 255 | xfs_buf_ioerror(bp, EFSCORRUPTED); |
| 256 | break; |
| 257 | } |
Dave Chinner | 612cfbf | 2012-11-14 17:52:32 +1100 | [diff] [blame] | 258 | } |
| 259 | |
Dave Chinner | b0f539d | 2012-11-14 17:53:49 +1100 | [diff] [blame] | 260 | static void |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame] | 261 | xfs_dir3_data_read_verify( |
Dave Chinner | 612cfbf | 2012-11-14 17:52:32 +1100 | [diff] [blame] | 262 | struct xfs_buf *bp) |
| 263 | { |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame] | 264 | struct xfs_mount *mp = bp->b_target->bt_mount; |
| 265 | |
| 266 | if ((xfs_sb_version_hascrc(&mp->m_sb) && |
| 267 | !xfs_verify_cksum(bp->b_addr, BBTOB(bp->b_length), |
| 268 | XFS_DIR3_DATA_CRC_OFF)) || |
| 269 | !xfs_dir3_data_verify(bp)) { |
| 270 | XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr); |
| 271 | xfs_buf_ioerror(bp, EFSCORRUPTED); |
| 272 | } |
Dave Chinner | e481357 | 2012-11-12 22:54:14 +1100 | [diff] [blame] | 273 | } |
| 274 | |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 275 | static void |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame] | 276 | xfs_dir3_data_write_verify( |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 277 | struct xfs_buf *bp) |
| 278 | { |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame] | 279 | struct xfs_mount *mp = bp->b_target->bt_mount; |
| 280 | struct xfs_buf_log_item *bip = bp->b_fspriv; |
| 281 | struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr; |
| 282 | |
| 283 | if (!xfs_dir3_data_verify(bp)) { |
| 284 | XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr); |
| 285 | xfs_buf_ioerror(bp, EFSCORRUPTED); |
| 286 | return; |
| 287 | } |
| 288 | |
| 289 | if (!xfs_sb_version_hascrc(&mp->m_sb)) |
| 290 | return; |
| 291 | |
| 292 | if (bip) |
| 293 | hdr3->lsn = cpu_to_be64(bip->bli_item.li_lsn); |
| 294 | |
| 295 | xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length), XFS_DIR3_DATA_CRC_OFF); |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 296 | } |
| 297 | |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame] | 298 | const struct xfs_buf_ops xfs_dir3_data_buf_ops = { |
| 299 | .verify_read = xfs_dir3_data_read_verify, |
| 300 | .verify_write = xfs_dir3_data_write_verify, |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 301 | }; |
| 302 | |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame] | 303 | static const struct xfs_buf_ops xfs_dir3_data_reada_buf_ops = { |
| 304 | .verify_read = xfs_dir3_data_reada_verify, |
| 305 | .verify_write = xfs_dir3_data_write_verify, |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 306 | }; |
| 307 | |
Dave Chinner | 612cfbf | 2012-11-14 17:52:32 +1100 | [diff] [blame] | 308 | |
Dave Chinner | e481357 | 2012-11-12 22:54:14 +1100 | [diff] [blame] | 309 | int |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame] | 310 | xfs_dir3_data_read( |
Dave Chinner | e481357 | 2012-11-12 22:54:14 +1100 | [diff] [blame] | 311 | struct xfs_trans *tp, |
| 312 | struct xfs_inode *dp, |
| 313 | xfs_dablk_t bno, |
| 314 | xfs_daddr_t mapped_bno, |
| 315 | struct xfs_buf **bpp) |
| 316 | { |
Dave Chinner | d75afeb | 2013-04-03 16:11:29 +1100 | [diff] [blame] | 317 | int err; |
| 318 | |
| 319 | err = xfs_da_read_buf(tp, dp, bno, mapped_bno, bpp, |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame] | 320 | XFS_DATA_FORK, &xfs_dir3_data_buf_ops); |
Dave Chinner | d75afeb | 2013-04-03 16:11:29 +1100 | [diff] [blame] | 321 | if (!err && tp) |
Dave Chinner | 61fe135 | 2013-04-03 16:11:30 +1100 | [diff] [blame] | 322 | xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_DATA_BUF); |
Dave Chinner | d75afeb | 2013-04-03 16:11:29 +1100 | [diff] [blame] | 323 | return err; |
Dave Chinner | e481357 | 2012-11-12 22:54:14 +1100 | [diff] [blame] | 324 | } |
| 325 | |
Dave Chinner | da6958c | 2012-11-12 22:54:18 +1100 | [diff] [blame] | 326 | int |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame] | 327 | xfs_dir3_data_readahead( |
Dave Chinner | da6958c | 2012-11-12 22:54:18 +1100 | [diff] [blame] | 328 | struct xfs_trans *tp, |
| 329 | struct xfs_inode *dp, |
| 330 | xfs_dablk_t bno, |
| 331 | xfs_daddr_t mapped_bno) |
| 332 | { |
| 333 | return xfs_da_reada_buf(tp, dp, bno, mapped_bno, |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame] | 334 | XFS_DATA_FORK, &xfs_dir3_data_reada_buf_ops); |
Dave Chinner | da6958c | 2012-11-12 22:54:18 +1100 | [diff] [blame] | 335 | } |
| 336 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | /* |
| 338 | * Given a data block and an unused entry from that block, |
| 339 | * return the bestfree entry if any that corresponds to it. |
| 340 | */ |
Dave Chinner | 2b9ab5a | 2013-08-12 20:49:37 +1000 | [diff] [blame] | 341 | xfs_dir2_data_free_t * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | xfs_dir2_data_freefind( |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 343 | xfs_dir2_data_hdr_t *hdr, /* data block */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | xfs_dir2_data_unused_t *dup) /* data unused entry */ |
| 345 | { |
| 346 | xfs_dir2_data_free_t *dfp; /* bestfree entry */ |
| 347 | xfs_dir2_data_aoff_t off; /* offset value needed */ |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 348 | struct xfs_dir2_data_free *bf; |
Dave Chinner | b49a0c1 | 2013-08-12 20:49:51 +1000 | [diff] [blame] | 349 | #ifdef DEBUG |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | int matched; /* matched the value */ |
| 351 | int seenzero; /* saw a 0 bestfree entry */ |
| 352 | #endif |
| 353 | |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 354 | off = (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr); |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 355 | bf = xfs_dir3_data_bestfree_p(hdr); |
| 356 | |
Dave Chinner | b49a0c1 | 2013-08-12 20:49:51 +1000 | [diff] [blame] | 357 | #ifdef DEBUG |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | /* |
| 359 | * Validate some consistency in the bestfree table. |
| 360 | * Check order, non-overlapping entries, and if we find the |
| 361 | * one we're looking for it has to be exact. |
| 362 | */ |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 363 | ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) || |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame] | 364 | hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) || |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 365 | hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) || |
| 366 | hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)); |
| 367 | for (dfp = &bf[0], seenzero = matched = 0; |
| 368 | dfp < &bf[XFS_DIR2_DATA_FD_COUNT]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | dfp++) { |
| 370 | if (!dfp->offset) { |
| 371 | ASSERT(!dfp->length); |
| 372 | seenzero = 1; |
| 373 | continue; |
| 374 | } |
| 375 | ASSERT(seenzero == 0); |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 376 | if (be16_to_cpu(dfp->offset) == off) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | matched = 1; |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 378 | ASSERT(dfp->length == dup->length); |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 379 | } else if (off < be16_to_cpu(dfp->offset)) |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 380 | ASSERT(off + be16_to_cpu(dup->length) <= be16_to_cpu(dfp->offset)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | else |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 382 | ASSERT(be16_to_cpu(dfp->offset) + be16_to_cpu(dfp->length) <= off); |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 383 | ASSERT(matched || be16_to_cpu(dfp->length) >= be16_to_cpu(dup->length)); |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 384 | if (dfp > &bf[0]) |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 385 | 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] | 386 | } |
| 387 | #endif |
| 388 | /* |
| 389 | * If this is smaller than the smallest bestfree entry, |
| 390 | * it can't be there since they're sorted. |
| 391 | */ |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 392 | if (be16_to_cpu(dup->length) < |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 393 | be16_to_cpu(bf[XFS_DIR2_DATA_FD_COUNT - 1].length)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | return NULL; |
| 395 | /* |
| 396 | * Look at the three bestfree entries for our guy. |
| 397 | */ |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 398 | for (dfp = &bf[0]; dfp < &bf[XFS_DIR2_DATA_FD_COUNT]; dfp++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | if (!dfp->offset) |
| 400 | return NULL; |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 401 | if (be16_to_cpu(dfp->offset) == off) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | return dfp; |
| 403 | } |
| 404 | /* |
| 405 | * Didn't find it. This only happens if there are duplicate lengths. |
| 406 | */ |
| 407 | return NULL; |
| 408 | } |
| 409 | |
| 410 | /* |
| 411 | * Insert an unused-space entry into the bestfree table. |
| 412 | */ |
| 413 | xfs_dir2_data_free_t * /* entry inserted */ |
| 414 | xfs_dir2_data_freeinsert( |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 415 | xfs_dir2_data_hdr_t *hdr, /* data block pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | xfs_dir2_data_unused_t *dup, /* unused space */ |
| 417 | int *loghead) /* log the data header (out) */ |
| 418 | { |
| 419 | xfs_dir2_data_free_t *dfp; /* bestfree table pointer */ |
| 420 | xfs_dir2_data_free_t new; /* new bestfree entry */ |
| 421 | |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 422 | ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) || |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 423 | hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) || |
| 424 | hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) || |
| 425 | hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)); |
| 426 | |
| 427 | dfp = xfs_dir3_data_bestfree_p(hdr); |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 428 | new.length = dup->length; |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 429 | new.offset = cpu_to_be16((char *)dup - (char *)hdr); |
| 430 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | /* |
| 432 | * Insert at position 0, 1, or 2; or not at all. |
| 433 | */ |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 434 | if (be16_to_cpu(new.length) > be16_to_cpu(dfp[0].length)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | dfp[2] = dfp[1]; |
| 436 | dfp[1] = dfp[0]; |
| 437 | dfp[0] = new; |
| 438 | *loghead = 1; |
| 439 | return &dfp[0]; |
| 440 | } |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 441 | if (be16_to_cpu(new.length) > be16_to_cpu(dfp[1].length)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | dfp[2] = dfp[1]; |
| 443 | dfp[1] = new; |
| 444 | *loghead = 1; |
| 445 | return &dfp[1]; |
| 446 | } |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 447 | if (be16_to_cpu(new.length) > be16_to_cpu(dfp[2].length)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | dfp[2] = new; |
| 449 | *loghead = 1; |
| 450 | return &dfp[2]; |
| 451 | } |
| 452 | return NULL; |
| 453 | } |
| 454 | |
| 455 | /* |
| 456 | * Remove a bestfree entry from the table. |
| 457 | */ |
Christoph Hellwig | ba0f32d | 2005-06-21 15:36:52 +1000 | [diff] [blame] | 458 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | xfs_dir2_data_freeremove( |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 460 | xfs_dir2_data_hdr_t *hdr, /* data block header */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | xfs_dir2_data_free_t *dfp, /* bestfree entry pointer */ |
| 462 | int *loghead) /* out: log data header */ |
| 463 | { |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 464 | struct xfs_dir2_data_free *bf; |
| 465 | |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 466 | ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) || |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 467 | hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) || |
| 468 | hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) || |
| 469 | hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)); |
| 470 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | /* |
| 472 | * It's the first entry, slide the next 2 up. |
| 473 | */ |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 474 | bf = xfs_dir3_data_bestfree_p(hdr); |
| 475 | if (dfp == &bf[0]) { |
| 476 | bf[0] = bf[1]; |
| 477 | bf[1] = bf[2]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | } |
| 479 | /* |
| 480 | * It's the second entry, slide the 3rd entry up. |
| 481 | */ |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 482 | else if (dfp == &bf[1]) |
| 483 | bf[1] = bf[2]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | /* |
| 485 | * Must be the last entry. |
| 486 | */ |
| 487 | else |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 488 | ASSERT(dfp == &bf[2]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | /* |
| 490 | * Clear the 3rd entry, must be zero now. |
| 491 | */ |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 492 | bf[2].length = 0; |
| 493 | bf[2].offset = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | *loghead = 1; |
| 495 | } |
| 496 | |
| 497 | /* |
| 498 | * Given a data block, reconstruct its bestfree map. |
| 499 | */ |
| 500 | void |
| 501 | xfs_dir2_data_freescan( |
Dave Chinner | 9d23fc8 | 2013-10-29 22:11:48 +1100 | [diff] [blame^] | 502 | struct xfs_inode *dp, |
| 503 | struct xfs_dir2_data_hdr *hdr, |
| 504 | int *loghead) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | { |
| 506 | xfs_dir2_block_tail_t *btp; /* block tail */ |
| 507 | xfs_dir2_data_entry_t *dep; /* active data entry */ |
| 508 | xfs_dir2_data_unused_t *dup; /* unused data entry */ |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 509 | struct xfs_dir2_data_free *bf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | char *endp; /* end of block's data */ |
| 511 | char *p; /* current entry pointer */ |
| 512 | |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 513 | ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) || |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame] | 514 | hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) || |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 515 | hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) || |
| 516 | hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)); |
| 517 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | /* |
| 519 | * Start by clearing the table. |
| 520 | */ |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 521 | bf = xfs_dir3_data_bestfree_p(hdr); |
| 522 | memset(bf, 0, sizeof(*bf) * XFS_DIR2_DATA_FD_COUNT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | *loghead = 1; |
| 524 | /* |
| 525 | * Set up pointers. |
| 526 | */ |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 527 | p = (char *)xfs_dir3_data_entry_p(hdr); |
| 528 | if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) || |
| 529 | hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) { |
Dave Chinner | 9d23fc8 | 2013-10-29 22:11:48 +1100 | [diff] [blame^] | 530 | btp = xfs_dir2_block_tail_p(dp->i_mount, hdr); |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 531 | endp = (char *)xfs_dir2_block_leaf_p(btp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | } else |
Dave Chinner | 9d23fc8 | 2013-10-29 22:11:48 +1100 | [diff] [blame^] | 533 | endp = (char *)hdr + dp->i_mount->m_dirblksize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | /* |
| 535 | * Loop over the block's entries. |
| 536 | */ |
| 537 | while (p < endp) { |
| 538 | dup = (xfs_dir2_data_unused_t *)p; |
| 539 | /* |
| 540 | * If it's a free entry, insert it. |
| 541 | */ |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 542 | if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) { |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 543 | ASSERT((char *)dup - (char *)hdr == |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 544 | be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup))); |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 545 | xfs_dir2_data_freeinsert(hdr, dup, loghead); |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 546 | p += be16_to_cpu(dup->length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | } |
| 548 | /* |
| 549 | * For active entries, check their tags and skip them. |
| 550 | */ |
| 551 | else { |
| 552 | dep = (xfs_dir2_data_entry_t *)p; |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 553 | ASSERT((char *)dep - (char *)hdr == |
Dave Chinner | 9d23fc8 | 2013-10-29 22:11:48 +1100 | [diff] [blame^] | 554 | be16_to_cpu(*dp->d_ops->data_entry_tag_p(dep))); |
| 555 | p += dp->d_ops->data_entsize(dep->namelen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | } |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | /* |
| 561 | * Initialize a data block at the given block number in the directory. |
| 562 | * Give back the buffer for the created block. |
| 563 | */ |
| 564 | int /* error */ |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 565 | xfs_dir3_data_init( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | xfs_da_args_t *args, /* directory operation args */ |
| 567 | xfs_dir2_db_t blkno, /* logical dir block number */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 568 | struct xfs_buf **bpp) /* output block buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | { |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 570 | struct xfs_buf *bp; /* block buffer */ |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 571 | xfs_dir2_data_hdr_t *hdr; /* data block header */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | xfs_inode_t *dp; /* incore directory inode */ |
| 573 | xfs_dir2_data_unused_t *dup; /* unused entry pointer */ |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 574 | struct xfs_dir2_data_free *bf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | int error; /* error return value */ |
| 576 | int i; /* bestfree index */ |
| 577 | xfs_mount_t *mp; /* filesystem mount point */ |
| 578 | xfs_trans_t *tp; /* transaction pointer */ |
| 579 | int t; /* temp */ |
| 580 | |
| 581 | dp = args->dp; |
| 582 | mp = dp->i_mount; |
| 583 | tp = args->trans; |
| 584 | /* |
| 585 | * Get the buffer set up for the block. |
| 586 | */ |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 587 | 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] | 588 | XFS_DATA_FORK); |
Dave Chinner | b0f539d | 2012-11-14 17:53:49 +1100 | [diff] [blame] | 589 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | return error; |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame] | 591 | bp->b_ops = &xfs_dir3_data_buf_ops; |
Dave Chinner | 61fe135 | 2013-04-03 16:11:30 +1100 | [diff] [blame] | 592 | xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_DATA_BUF); |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 593 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | /* |
| 595 | * Initialize the header. |
| 596 | */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 597 | hdr = bp->b_addr; |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 598 | if (xfs_sb_version_hascrc(&mp->m_sb)) { |
| 599 | struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr; |
| 600 | |
| 601 | memset(hdr3, 0, sizeof(*hdr3)); |
| 602 | hdr3->magic = cpu_to_be32(XFS_DIR3_DATA_MAGIC); |
| 603 | hdr3->blkno = cpu_to_be64(bp->b_bn); |
| 604 | hdr3->owner = cpu_to_be64(dp->i_ino); |
| 605 | uuid_copy(&hdr3->uuid, &mp->m_sb.sb_uuid); |
| 606 | |
| 607 | } else |
| 608 | hdr->magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC); |
| 609 | |
| 610 | bf = xfs_dir3_data_bestfree_p(hdr); |
| 611 | bf[0].offset = cpu_to_be16(xfs_dir3_data_entry_offset(hdr)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | for (i = 1; i < XFS_DIR2_DATA_FD_COUNT; i++) { |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 613 | bf[i].length = 0; |
| 614 | bf[i].offset = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | } |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 616 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | /* |
| 618 | * Set up an unused entry for the block's body. |
| 619 | */ |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 620 | dup = xfs_dir3_data_unused_p(hdr); |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 621 | dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 623 | t = mp->m_dirblksize - (uint)xfs_dir3_data_entry_offset(hdr); |
| 624 | bf[0].length = cpu_to_be16(t); |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 625 | dup->length = cpu_to_be16(t); |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 626 | *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] | 627 | /* |
| 628 | * Log it and return it. |
| 629 | */ |
| 630 | xfs_dir2_data_log_header(tp, bp); |
| 631 | xfs_dir2_data_log_unused(tp, bp, dup); |
| 632 | *bpp = bp; |
| 633 | return 0; |
| 634 | } |
| 635 | |
| 636 | /* |
| 637 | * Log an active data entry from the block. |
| 638 | */ |
| 639 | void |
| 640 | xfs_dir2_data_log_entry( |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 641 | struct xfs_trans *tp, |
Dave Chinner | 9d23fc8 | 2013-10-29 22:11:48 +1100 | [diff] [blame^] | 642 | struct xfs_inode *dp, |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 643 | struct xfs_buf *bp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | xfs_dir2_data_entry_t *dep) /* data entry pointer */ |
| 645 | { |
Dave Chinner | 0cb9776 | 2013-08-12 20:50:09 +1000 | [diff] [blame] | 646 | struct xfs_dir2_data_hdr *hdr = bp->b_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 648 | ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) || |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame] | 649 | hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) || |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 650 | hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) || |
| 651 | hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)); |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 652 | |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 653 | xfs_trans_log_buf(tp, bp, (uint)((char *)dep - (char *)hdr), |
Dave Chinner | 9d23fc8 | 2013-10-29 22:11:48 +1100 | [diff] [blame^] | 654 | (uint)((char *)(dp->d_ops->data_entry_tag_p(dep) + 1) - |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 655 | (char *)hdr - 1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | /* |
| 659 | * Log a data block header. |
| 660 | */ |
| 661 | void |
| 662 | xfs_dir2_data_log_header( |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 663 | struct xfs_trans *tp, |
| 664 | struct xfs_buf *bp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | { |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 666 | xfs_dir2_data_hdr_t *hdr = bp->b_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 668 | ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) || |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame] | 669 | hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) || |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 670 | hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) || |
| 671 | hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)); |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 672 | |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 673 | xfs_trans_log_buf(tp, bp, 0, xfs_dir3_data_entry_offset(hdr) - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | /* |
| 677 | * Log a data unused entry. |
| 678 | */ |
| 679 | void |
| 680 | xfs_dir2_data_log_unused( |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 681 | struct xfs_trans *tp, |
| 682 | struct xfs_buf *bp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | xfs_dir2_data_unused_t *dup) /* data unused pointer */ |
| 684 | { |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 685 | xfs_dir2_data_hdr_t *hdr = bp->b_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 687 | ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) || |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame] | 688 | hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) || |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 689 | hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) || |
| 690 | hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)); |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 691 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | /* |
| 693 | * Log the first part of the unused entry. |
| 694 | */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 695 | xfs_trans_log_buf(tp, bp, (uint)((char *)dup - (char *)hdr), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | (uint)((char *)&dup->length + sizeof(dup->length) - |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 697 | 1 - (char *)hdr)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | /* |
| 699 | * Log the end (tag) of the unused entry. |
| 700 | */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 701 | xfs_trans_log_buf(tp, bp, |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 702 | (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr), |
| 703 | (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr + |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | sizeof(xfs_dir2_data_off_t) - 1)); |
| 705 | } |
| 706 | |
| 707 | /* |
| 708 | * Make a byte range in the data block unused. |
| 709 | * Its current contents are unimportant. |
| 710 | */ |
| 711 | void |
| 712 | xfs_dir2_data_make_free( |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 713 | struct xfs_trans *tp, |
| 714 | struct xfs_buf *bp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | xfs_dir2_data_aoff_t offset, /* starting byte offset */ |
| 716 | xfs_dir2_data_aoff_t len, /* length in bytes */ |
| 717 | int *needlogp, /* out: log header */ |
| 718 | int *needscanp) /* out: regen bestfree */ |
| 719 | { |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 720 | xfs_dir2_data_hdr_t *hdr; /* data block pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | xfs_dir2_data_free_t *dfp; /* bestfree pointer */ |
| 722 | char *endptr; /* end of data area */ |
| 723 | xfs_mount_t *mp; /* filesystem mount point */ |
| 724 | int needscan; /* need to regen bestfree */ |
| 725 | xfs_dir2_data_unused_t *newdup; /* new unused entry */ |
| 726 | xfs_dir2_data_unused_t *postdup; /* unused entry after us */ |
| 727 | xfs_dir2_data_unused_t *prevdup; /* unused entry before us */ |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 728 | struct xfs_dir2_data_free *bf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | |
| 730 | mp = tp->t_mountp; |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 731 | hdr = bp->b_addr; |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 732 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | /* |
| 734 | * Figure out where the end of the data area is. |
| 735 | */ |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame] | 736 | if (hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) || |
| 737 | hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC)) |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 738 | endptr = (char *)hdr + mp->m_dirblksize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | else { |
| 740 | xfs_dir2_block_tail_t *btp; /* block tail */ |
| 741 | |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 742 | ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) || |
| 743 | hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)); |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 744 | btp = xfs_dir2_block_tail_p(mp, hdr); |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 745 | endptr = (char *)xfs_dir2_block_leaf_p(btp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | } |
| 747 | /* |
| 748 | * If this isn't the start of the block, then back up to |
| 749 | * the previous entry and see if it's free. |
| 750 | */ |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 751 | if (offset > xfs_dir3_data_entry_offset(hdr)) { |
Nathan Scott | 3d693c6 | 2006-03-17 17:28:27 +1100 | [diff] [blame] | 752 | __be16 *tagp; /* tag just before us */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 754 | tagp = (__be16 *)((char *)hdr + offset) - 1; |
| 755 | prevdup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp)); |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 756 | if (be16_to_cpu(prevdup->freetag) != XFS_DIR2_DATA_FREE_TAG) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | prevdup = NULL; |
| 758 | } else |
| 759 | prevdup = NULL; |
| 760 | /* |
| 761 | * If this isn't the end of the block, see if the entry after |
| 762 | * us is free. |
| 763 | */ |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 764 | if ((char *)hdr + offset + len < endptr) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | postdup = |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 766 | (xfs_dir2_data_unused_t *)((char *)hdr + offset + len); |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 767 | if (be16_to_cpu(postdup->freetag) != XFS_DIR2_DATA_FREE_TAG) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | postdup = NULL; |
| 769 | } else |
| 770 | postdup = NULL; |
| 771 | ASSERT(*needscanp == 0); |
| 772 | needscan = 0; |
| 773 | /* |
| 774 | * Previous and following entries are both free, |
| 775 | * merge everything into a single free entry. |
| 776 | */ |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 777 | bf = xfs_dir3_data_bestfree_p(hdr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | if (prevdup && postdup) { |
| 779 | xfs_dir2_data_free_t *dfp2; /* another bestfree pointer */ |
| 780 | |
| 781 | /* |
| 782 | * See if prevdup and/or postdup are in bestfree table. |
| 783 | */ |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 784 | dfp = xfs_dir2_data_freefind(hdr, prevdup); |
| 785 | dfp2 = xfs_dir2_data_freefind(hdr, postdup); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | /* |
| 787 | * We need a rescan unless there are exactly 2 free entries |
| 788 | * namely our two. Then we know what's happening, otherwise |
| 789 | * since the third bestfree is there, there might be more |
| 790 | * entries. |
| 791 | */ |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 792 | needscan = (bf[2].length != 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | /* |
| 794 | * Fix up the new big freespace. |
| 795 | */ |
Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 796 | be16_add_cpu(&prevdup->length, len + be16_to_cpu(postdup->length)); |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 797 | *xfs_dir2_data_unused_tag_p(prevdup) = |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 798 | cpu_to_be16((char *)prevdup - (char *)hdr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | xfs_dir2_data_log_unused(tp, bp, prevdup); |
| 800 | if (!needscan) { |
| 801 | /* |
| 802 | * Has to be the case that entries 0 and 1 are |
| 803 | * dfp and dfp2 (don't know which is which), and |
| 804 | * entry 2 is empty. |
| 805 | * Remove entry 1 first then entry 0. |
| 806 | */ |
| 807 | ASSERT(dfp && dfp2); |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 808 | if (dfp == &bf[1]) { |
| 809 | dfp = &bf[0]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | ASSERT(dfp2 == dfp); |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 811 | dfp2 = &bf[1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | } |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 813 | xfs_dir2_data_freeremove(hdr, dfp2, needlogp); |
| 814 | xfs_dir2_data_freeremove(hdr, dfp, needlogp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | /* |
| 816 | * Now insert the new entry. |
| 817 | */ |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 818 | dfp = xfs_dir2_data_freeinsert(hdr, prevdup, needlogp); |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 819 | ASSERT(dfp == &bf[0]); |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 820 | ASSERT(dfp->length == prevdup->length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | ASSERT(!dfp[1].length); |
| 822 | ASSERT(!dfp[2].length); |
| 823 | } |
| 824 | } |
| 825 | /* |
| 826 | * The entry before us is free, merge with it. |
| 827 | */ |
| 828 | else if (prevdup) { |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 829 | dfp = xfs_dir2_data_freefind(hdr, prevdup); |
Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 830 | be16_add_cpu(&prevdup->length, len); |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 831 | *xfs_dir2_data_unused_tag_p(prevdup) = |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 832 | cpu_to_be16((char *)prevdup - (char *)hdr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 833 | xfs_dir2_data_log_unused(tp, bp, prevdup); |
| 834 | /* |
| 835 | * If the previous entry was in the table, the new entry |
| 836 | * is longer, so it will be in the table too. Remove |
| 837 | * the old one and add the new one. |
| 838 | */ |
| 839 | if (dfp) { |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 840 | xfs_dir2_data_freeremove(hdr, dfp, needlogp); |
| 841 | xfs_dir2_data_freeinsert(hdr, prevdup, needlogp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 | } |
| 843 | /* |
| 844 | * Otherwise we need a scan if the new entry is big enough. |
| 845 | */ |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 846 | else { |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 847 | needscan = be16_to_cpu(prevdup->length) > |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 848 | be16_to_cpu(bf[2].length); |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 849 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 850 | } |
| 851 | /* |
| 852 | * The following entry is free, merge with it. |
| 853 | */ |
| 854 | else if (postdup) { |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 855 | dfp = xfs_dir2_data_freefind(hdr, postdup); |
| 856 | newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset); |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 857 | newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG); |
| 858 | newdup->length = cpu_to_be16(len + be16_to_cpu(postdup->length)); |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 859 | *xfs_dir2_data_unused_tag_p(newdup) = |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 860 | cpu_to_be16((char *)newdup - (char *)hdr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 861 | xfs_dir2_data_log_unused(tp, bp, newdup); |
| 862 | /* |
| 863 | * If the following entry was in the table, the new entry |
| 864 | * is longer, so it will be in the table too. Remove |
| 865 | * the old one and add the new one. |
| 866 | */ |
| 867 | if (dfp) { |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 868 | xfs_dir2_data_freeremove(hdr, dfp, needlogp); |
| 869 | xfs_dir2_data_freeinsert(hdr, newdup, needlogp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | } |
| 871 | /* |
| 872 | * Otherwise we need a scan if the new entry is big enough. |
| 873 | */ |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 874 | else { |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 875 | needscan = be16_to_cpu(newdup->length) > |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 876 | be16_to_cpu(bf[2].length); |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 877 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | } |
| 879 | /* |
| 880 | * Neither neighbor is free. Make a new entry. |
| 881 | */ |
| 882 | else { |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 883 | newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset); |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 884 | newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG); |
| 885 | newdup->length = cpu_to_be16(len); |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 886 | *xfs_dir2_data_unused_tag_p(newdup) = |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 887 | cpu_to_be16((char *)newdup - (char *)hdr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | xfs_dir2_data_log_unused(tp, bp, newdup); |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 889 | xfs_dir2_data_freeinsert(hdr, newdup, needlogp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 890 | } |
| 891 | *needscanp = needscan; |
| 892 | } |
| 893 | |
| 894 | /* |
| 895 | * Take a byte range out of an existing unused space and make it un-free. |
| 896 | */ |
| 897 | void |
| 898 | xfs_dir2_data_use_free( |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 899 | struct xfs_trans *tp, |
| 900 | struct xfs_buf *bp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | xfs_dir2_data_unused_t *dup, /* unused entry */ |
| 902 | xfs_dir2_data_aoff_t offset, /* starting offset to use */ |
| 903 | xfs_dir2_data_aoff_t len, /* length to use */ |
| 904 | int *needlogp, /* out: need to log header */ |
| 905 | int *needscanp) /* out: need regen bestfree */ |
| 906 | { |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 907 | xfs_dir2_data_hdr_t *hdr; /* data block header */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 908 | xfs_dir2_data_free_t *dfp; /* bestfree pointer */ |
| 909 | int matchback; /* matches end of freespace */ |
| 910 | int matchfront; /* matches start of freespace */ |
| 911 | int needscan; /* need to regen bestfree */ |
| 912 | xfs_dir2_data_unused_t *newdup; /* new unused entry */ |
| 913 | xfs_dir2_data_unused_t *newdup2; /* another new unused entry */ |
| 914 | int oldlen; /* old unused entry's length */ |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 915 | struct xfs_dir2_data_free *bf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 916 | |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 917 | hdr = bp->b_addr; |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 918 | ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) || |
Dave Chinner | 33363fe | 2013-04-03 16:11:22 +1100 | [diff] [blame] | 919 | hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) || |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 920 | hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) || |
| 921 | hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)); |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 922 | ASSERT(be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG); |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 923 | ASSERT(offset >= (char *)dup - (char *)hdr); |
| 924 | ASSERT(offset + len <= (char *)dup + be16_to_cpu(dup->length) - (char *)hdr); |
| 925 | 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] | 926 | /* |
| 927 | * Look up the entry in the bestfree table. |
| 928 | */ |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 929 | dfp = xfs_dir2_data_freefind(hdr, dup); |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 930 | oldlen = be16_to_cpu(dup->length); |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 931 | bf = xfs_dir3_data_bestfree_p(hdr); |
| 932 | ASSERT(dfp || oldlen <= be16_to_cpu(bf[2].length)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 | /* |
| 934 | * Check for alignment with front and back of the entry. |
| 935 | */ |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 936 | matchfront = (char *)dup - (char *)hdr == offset; |
| 937 | matchback = (char *)dup + oldlen - (char *)hdr == offset + len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | ASSERT(*needscanp == 0); |
| 939 | needscan = 0; |
| 940 | /* |
| 941 | * If we matched it exactly we just need to get rid of it from |
| 942 | * the bestfree table. |
| 943 | */ |
| 944 | if (matchfront && matchback) { |
| 945 | if (dfp) { |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 946 | needscan = (bf[2].offset != 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | if (!needscan) |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 948 | xfs_dir2_data_freeremove(hdr, dfp, needlogp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | } |
| 950 | } |
| 951 | /* |
| 952 | * We match the first part of the entry. |
| 953 | * Make a new entry with the remaining freespace. |
| 954 | */ |
| 955 | else if (matchfront) { |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 956 | newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len); |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 957 | newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG); |
| 958 | newdup->length = cpu_to_be16(oldlen - len); |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 959 | *xfs_dir2_data_unused_tag_p(newdup) = |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 960 | cpu_to_be16((char *)newdup - (char *)hdr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | xfs_dir2_data_log_unused(tp, bp, newdup); |
| 962 | /* |
| 963 | * If it was in the table, remove it and add the new one. |
| 964 | */ |
| 965 | if (dfp) { |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 966 | xfs_dir2_data_freeremove(hdr, dfp, needlogp); |
| 967 | dfp = xfs_dir2_data_freeinsert(hdr, newdup, needlogp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 968 | ASSERT(dfp != NULL); |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 969 | ASSERT(dfp->length == newdup->length); |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 970 | ASSERT(be16_to_cpu(dfp->offset) == (char *)newdup - (char *)hdr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 971 | /* |
| 972 | * If we got inserted at the last slot, |
| 973 | * that means we don't know if there was a better |
| 974 | * choice for the last slot, or not. Rescan. |
| 975 | */ |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 976 | needscan = dfp == &bf[2]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 977 | } |
| 978 | } |
| 979 | /* |
| 980 | * We match the last part of the entry. |
| 981 | * Trim the allocated space off the tail of the entry. |
| 982 | */ |
| 983 | else if (matchback) { |
| 984 | newdup = dup; |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 985 | newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup); |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 986 | *xfs_dir2_data_unused_tag_p(newdup) = |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 987 | cpu_to_be16((char *)newdup - (char *)hdr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 | xfs_dir2_data_log_unused(tp, bp, newdup); |
| 989 | /* |
| 990 | * If it was in the table, remove it and add the new one. |
| 991 | */ |
| 992 | if (dfp) { |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 993 | xfs_dir2_data_freeremove(hdr, dfp, needlogp); |
| 994 | dfp = xfs_dir2_data_freeinsert(hdr, newdup, needlogp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | ASSERT(dfp != NULL); |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 996 | ASSERT(dfp->length == newdup->length); |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 997 | ASSERT(be16_to_cpu(dfp->offset) == (char *)newdup - (char *)hdr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | /* |
| 999 | * If we got inserted at the last slot, |
| 1000 | * that means we don't know if there was a better |
| 1001 | * choice for the last slot, or not. Rescan. |
| 1002 | */ |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 1003 | needscan = dfp == &bf[2]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1004 | } |
| 1005 | } |
| 1006 | /* |
| 1007 | * Poking out the middle of an entry. |
| 1008 | * Make two new entries. |
| 1009 | */ |
| 1010 | else { |
| 1011 | newdup = dup; |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 1012 | newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup); |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1013 | *xfs_dir2_data_unused_tag_p(newdup) = |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 1014 | cpu_to_be16((char *)newdup - (char *)hdr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | xfs_dir2_data_log_unused(tp, bp, newdup); |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 1016 | newdup2 = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len); |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 1017 | newdup2->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG); |
| 1018 | newdup2->length = cpu_to_be16(oldlen - len - be16_to_cpu(newdup->length)); |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1019 | *xfs_dir2_data_unused_tag_p(newdup2) = |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 1020 | cpu_to_be16((char *)newdup2 - (char *)hdr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1021 | xfs_dir2_data_log_unused(tp, bp, newdup2); |
| 1022 | /* |
| 1023 | * If the old entry was in the table, we need to scan |
| 1024 | * if the 3rd entry was valid, since these entries |
| 1025 | * are smaller than the old one. |
| 1026 | * If we don't need to scan that means there were 1 or 2 |
| 1027 | * entries in the table, and removing the old and adding |
| 1028 | * the 2 new will work. |
| 1029 | */ |
| 1030 | if (dfp) { |
Dave Chinner | f5f3d9b | 2013-04-03 16:11:20 +1100 | [diff] [blame] | 1031 | needscan = (bf[2].length != 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1032 | if (!needscan) { |
Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 1033 | xfs_dir2_data_freeremove(hdr, dfp, needlogp); |
| 1034 | xfs_dir2_data_freeinsert(hdr, newdup, needlogp); |
| 1035 | xfs_dir2_data_freeinsert(hdr, newdup2, |
| 1036 | needlogp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1037 | } |
| 1038 | } |
| 1039 | } |
| 1040 | *needscanp = needscan; |
| 1041 | } |