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