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