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