blob: cb67ec730b9bd50caf46e6dcd35e1e76aef46203 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +11003 * Copyright (c) 2013 Red Hat, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11004 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
Nathan Scott7b718762005-11-02 14:58:39 +11006 * 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 Torvalds1da177e2005-04-16 15:20:36 -07008 * published by the Free Software Foundation.
9 *
Nathan Scott7b718762005-11-02 14:58:39 +110010 * 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 Torvalds1da177e2005-04-16 15:20:36 -070014 *
Nathan Scott7b718762005-11-02 14:58:39 +110015 * 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 Torvalds1da177e2005-04-16 15:20:36 -070018 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110020#include "xfs_fs.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110021#include "xfs_format.h"
Dave Chinner239880e2013-10-23 10:50:10 +110022#include "xfs_log_format.h"
23#include "xfs_trans_resv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_mount.h"
Dave Chinner57062782013-10-15 09:17:51 +110025#include "xfs_da_format.h"
Nathan Scotta844f452005-11-02 14:38:42 +110026#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_inode.h"
Dave Chinner2b9ab5a2013-08-12 20:49:37 +100028#include "xfs_dir2.h"
Christoph Hellwig57926642011-07-13 13:43:48 +020029#include "xfs_dir2_priv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "xfs_error.h"
Dave Chinner239880e2013-10-23 10:50:10 +110031#include "xfs_trans.h"
Dave Chinner33363fe2013-04-03 16:11:22 +110032#include "xfs_buf_item.h"
33#include "xfs_cksum.h"
Brian Fostera45086e2015-10-12 15:59:25 +110034#include "xfs_log.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Linus Torvalds1da177e2005-04-16 15:20:36 -070036/*
37 * Check the consistency of the data block.
38 * The input can also be a block-format directory.
Darrick J. Wonga6a781a2018-01-08 10:51:03 -080039 * Return NULL if the buffer is good, otherwise the address of the error.
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 */
Darrick J. Wonga6a781a2018-01-08 10:51:03 -080041xfs_failaddr_t
Dave Chinner33363fe2013-04-03 16:11:22 +110042__xfs_dir3_data_check(
Dave Chinner1d9025e2012-06-22 18:50:14 +100043 struct xfs_inode *dp, /* incore inode pointer */
44 struct xfs_buf *bp) /* data block's buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
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 Hellwigc2066e22011-07-08 14:35:38 +020050 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 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 Naujok5163f952008-05-21 16:41:01 +100063 struct xfs_name name;
Dave Chinner9d23fc82013-10-29 22:11:48 +110064 const struct xfs_dir_ops *ops;
Dave Chinner7dda6e82014-06-06 15:11:18 +100065 struct xfs_da_geometry *geo;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Dave Chinner82025d72012-11-12 22:54:12 +110067 mp = bp->b_target->bt_mount;
Dave Chinner7dda6e82014-06-06 15:11:18 +100068 geo = mp->m_dir_geo;
Christoph Hellwigc2066e22011-07-08 14:35:38 +020069
Dave Chinner9d23fc82013-10-29 22:11:48 +110070 /*
Dave Chinner41419562013-10-29 22:11:50 +110071 * We can be passed a null dp here from a verifier, so we need to go the
72 * hard way to get them.
Dave Chinner9d23fc82013-10-29 22:11:48 +110073 */
Dave Chinner41419562013-10-29 22:11:50 +110074 ops = xfs_dir_get_ops(mp, dp);
Dave Chinner9d23fc82013-10-29 22:11:48 +110075
Darrick J. Wong46c59732018-01-09 11:11:42 -080076 /*
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 Chinnera6293622013-10-29 22:11:56 +110084 hdr = bp->b_addr;
85 p = (char *)ops->data_entry_p(hdr);
86
Dave Chinner82025d72012-11-12 22:54:12 +110087 switch (hdr->magic) {
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +110088 case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC):
Dave Chinner82025d72012-11-12 22:54:12 +110089 case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC):
Dave Chinner8f661932014-06-06 15:15:59 +100090 btp = xfs_dir2_block_tail_p(geo, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +100091 lep = xfs_dir2_block_leaf_p(btp);
Dave Chinnera6293622013-10-29 22:11:56 +110092
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. Wong9101d372018-01-08 10:51:01 -0800100 if (be32_to_cpu(btp->count) >=
101 ((char *)btp - p) / sizeof(struct xfs_dir2_leaf_entry))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800102 return __this_address;
Dave Chinner82025d72012-11-12 22:54:12 +1100103 break;
Dave Chinner33363fe2013-04-03 16:11:22 +1100104 case cpu_to_be32(XFS_DIR3_DATA_MAGIC):
Dave Chinner82025d72012-11-12 22:54:12 +1100105 case cpu_to_be32(XFS_DIR2_DATA_MAGIC):
Dave Chinner82025d72012-11-12 22:54:12 +1100106 break;
107 default:
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800108 return __this_address;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200109 }
Darrick J. Wongce92d292018-01-16 18:54:12 -0800110 endp = xfs_dir3_data_endp(geo, hdr);
111 if (!endp)
112 return __this_address;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 /*
115 * Account for zero bestfree entries.
116 */
Dave Chinnera6293622013-10-29 22:11:56 +1100117 bf = ops->data_bestfree_p(hdr);
118 count = lastfree = freeseen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 if (!bf[0].length) {
Darrick J. Wong9101d372018-01-08 10:51:01 -0800120 if (bf[0].offset)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800121 return __this_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 freeseen |= 1 << 0;
123 }
124 if (!bf[1].length) {
Darrick J. Wong9101d372018-01-08 10:51:01 -0800125 if (bf[1].offset)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800126 return __this_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 freeseen |= 1 << 1;
128 }
129 if (!bf[2].length) {
Darrick J. Wong9101d372018-01-08 10:51:01 -0800130 if (bf[2].offset)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800131 return __this_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 freeseen |= 1 << 2;
133 }
Dave Chinner82025d72012-11-12 22:54:12 +1100134
Darrick J. Wong9101d372018-01-08 10:51:01 -0800135 if (be16_to_cpu(bf[0].length) < be16_to_cpu(bf[1].length))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800136 return __this_address;
Darrick J. Wong9101d372018-01-08 10:51:01 -0800137 if (be16_to_cpu(bf[1].length) < be16_to_cpu(bf[2].length))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800138 return __this_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 /*
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 Scottad354eb2006-03-17 17:27:37 +1100149 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
Darrick J. Wong9101d372018-01-08 10:51:01 -0800150 if (lastfree != 0)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800151 return __this_address;
Darrick J. Wong9101d372018-01-08 10:51:01 -0800152 if (endp < p + be16_to_cpu(dup->length))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800153 return __this_address;
Darrick J. Wong9101d372018-01-08 10:51:01 -0800154 if (be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)) !=
155 (char *)dup - (char *)hdr)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800156 return __this_address;
Dave Chinner2ca98772013-10-29 22:11:49 +1100157 dfp = xfs_dir2_data_freefind(hdr, bf, dup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 if (dfp) {
159 i = (int)(dfp - bf);
Darrick J. Wong9101d372018-01-08 10:51:01 -0800160 if ((freeseen & (1 << i)) != 0)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800161 return __this_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 freeseen |= 1 << i;
Nathan Scott70e73f52006-03-17 17:26:52 +1100163 } else {
Darrick J. Wong9101d372018-01-08 10:51:01 -0800164 if (be16_to_cpu(dup->length) >
165 be16_to_cpu(bf[2].length))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800166 return __this_address;
Nathan Scott70e73f52006-03-17 17:26:52 +1100167 }
Nathan Scottad354eb2006-03-17 17:27:37 +1100168 p += be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 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. Wong9101d372018-01-08 10:51:01 -0800179 if (dep->namelen == 0)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800180 return __this_address;
Darrick J. Wong9101d372018-01-08 10:51:01 -0800181 if (xfs_dir_ino_validate(mp, be64_to_cpu(dep->inumber)))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800182 return __this_address;
Darrick J. Wong9101d372018-01-08 10:51:01 -0800183 if (endp < p + ops->data_entsize(dep->namelen))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800184 return __this_address;
Darrick J. Wong9101d372018-01-08 10:51:01 -0800185 if (be16_to_cpu(*ops->data_entry_tag_p(dep)) !=
186 (char *)dep - (char *)hdr)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800187 return __this_address;
Darrick J. Wong9101d372018-01-08 10:51:01 -0800188 if (ops->data_get_ftype(dep) >= XFS_DIR3_FT_MAX)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800189 return __this_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 count++;
191 lastfree = 0;
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100192 if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
193 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) {
Dave Chinner7dda6e82014-06-06 15:11:18 +1000194 addr = xfs_dir2_db_off_to_dataptr(geo, geo->datablk,
195 (xfs_dir2_data_aoff_t)
196 ((char *)dep - (char *)hdr));
Barry Naujok5163f952008-05-21 16:41:01 +1000197 name.name = dep->name;
198 name.len = dep->namelen;
199 hash = mp->m_dirnameops->hashname(&name);
Nathan Scotte922fff2006-03-17 17:27:56 +1100200 for (i = 0; i < be32_to_cpu(btp->count); i++) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100201 if (be32_to_cpu(lep[i].address) == addr &&
202 be32_to_cpu(lep[i].hashval) == hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 break;
204 }
Darrick J. Wong9101d372018-01-08 10:51:01 -0800205 if (i >= be32_to_cpu(btp->count))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800206 return __this_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 }
Dave Chinner9d23fc82013-10-29 22:11:48 +1100208 p += ops->data_entsize(dep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 }
210 /*
211 * Need to have seen all the entries and all the bestfree slots.
212 */
Darrick J. Wong9101d372018-01-08 10:51:01 -0800213 if (freeseen != 7)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800214 return __this_address;
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100215 if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
216 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) {
Nathan Scotte922fff2006-03-17 17:27:56 +1100217 for (i = stale = 0; i < be32_to_cpu(btp->count); i++) {
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200218 if (lep[i].address ==
219 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 stale++;
Darrick J. Wong9101d372018-01-08 10:51:01 -0800221 if (i > 0 && be32_to_cpu(lep[i].hashval) <
222 be32_to_cpu(lep[i - 1].hashval))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800223 return __this_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 }
Darrick J. Wong9101d372018-01-08 10:51:01 -0800225 if (count != be32_to_cpu(btp->count) - be32_to_cpu(btp->stale))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800226 return __this_address;
Darrick J. Wong9101d372018-01-08 10:51:01 -0800227 if (stale != be32_to_cpu(btp->stale))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800228 return __this_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 }
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800230 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800233#ifdef DEBUG
234void
235xfs_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
250static xfs_failaddr_t
Dave Chinner33363fe2013-04-03 16:11:22 +1100251xfs_dir3_data_verify(
Dave Chinnere4813572012-11-12 22:54:14 +1100252 struct xfs_buf *bp)
253{
254 struct xfs_mount *mp = bp->b_target->bt_mount;
Dave Chinner33363fe2013-04-03 16:11:22 +1100255 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
Dave Chinnere4813572012-11-12 22:54:14 +1100256
Dave Chinner33363fe2013-04-03 16:11:22 +1100257 if (xfs_sb_version_hascrc(&mp->m_sb)) {
258 if (hdr3->magic != cpu_to_be32(XFS_DIR3_DATA_MAGIC))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800259 return __this_address;
Eric Sandeence748ea2015-07-29 11:53:31 +1000260 if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_meta_uuid))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800261 return __this_address;
Dave Chinner33363fe2013-04-03 16:11:22 +1100262 if (be64_to_cpu(hdr3->blkno) != bp->b_bn)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800263 return __this_address;
Brian Fostera45086e2015-10-12 15:59:25 +1100264 if (!xfs_log_check_lsn(mp, be64_to_cpu(hdr3->lsn)))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800265 return __this_address;
Dave Chinner33363fe2013-04-03 16:11:22 +1100266 } else {
267 if (hdr3->magic != cpu_to_be32(XFS_DIR2_DATA_MAGIC))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800268 return __this_address;
Dave Chinnere4813572012-11-12 22:54:14 +1100269 }
Darrick J. Wong9101d372018-01-08 10:51:01 -0800270 return __xfs_dir3_data_check(NULL, bp);
Dave Chinner612cfbf2012-11-14 17:52:32 +1100271}
Dave Chinnere4813572012-11-12 22:54:14 +1100272
Dave Chinner1813dd62012-11-14 17:54:40 +1100273/*
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 */
278static void
Dave Chinner33363fe2013-04-03 16:11:22 +1100279xfs_dir3_data_reada_verify(
Dave Chinner1813dd62012-11-14 17:54:40 +1100280 struct xfs_buf *bp)
Dave Chinner612cfbf2012-11-14 17:52:32 +1100281{
Dave Chinner1813dd62012-11-14 17:54:40 +1100282 struct xfs_dir2_data_hdr *hdr = bp->b_addr;
283
284 switch (hdr->magic) {
285 case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC):
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100286 case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC):
287 bp->b_ops = &xfs_dir3_block_buf_ops;
Dave Chinner1813dd62012-11-14 17:54:40 +1100288 bp->b_ops->verify_read(bp);
289 return;
290 case cpu_to_be32(XFS_DIR2_DATA_MAGIC):
Dave Chinner33363fe2013-04-03 16:11:22 +1100291 case cpu_to_be32(XFS_DIR3_DATA_MAGIC):
Darrick J. Wong2f123bc2015-08-19 10:33:58 +1000292 bp->b_ops = &xfs_dir3_data_buf_ops;
293 bp->b_ops->verify_read(bp);
Dave Chinner1813dd62012-11-14 17:54:40 +1100294 return;
295 default:
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800296 xfs_verifier_error(bp, -EFSCORRUPTED, __this_address);
Dave Chinner1813dd62012-11-14 17:54:40 +1100297 break;
298 }
Dave Chinner612cfbf2012-11-14 17:52:32 +1100299}
300
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100301static void
Dave Chinner33363fe2013-04-03 16:11:22 +1100302xfs_dir3_data_read_verify(
Dave Chinner612cfbf2012-11-14 17:52:32 +1100303 struct xfs_buf *bp)
304{
Dave Chinner33363fe2013-04-03 16:11:22 +1100305 struct xfs_mount *mp = bp->b_target->bt_mount;
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800306 xfs_failaddr_t fa;
Dave Chinner33363fe2013-04-03 16:11:22 +1100307
Eric Sandeence5028c2014-02-27 15:23:10 +1100308 if (xfs_sb_version_hascrc(&mp->m_sb) &&
Darrick J. Wong31ca03c2018-01-08 10:51:02 -0800309 !xfs_buf_verify_cksum(bp, XFS_DIR3_DATA_CRC_OFF))
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800310 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 Chinnere4813572012-11-12 22:54:14 +1100316}
317
Dave Chinner1813dd62012-11-14 17:54:40 +1100318static void
Dave Chinner33363fe2013-04-03 16:11:22 +1100319xfs_dir3_data_write_verify(
Dave Chinner1813dd62012-11-14 17:54:40 +1100320 struct xfs_buf *bp)
321{
Dave Chinner33363fe2013-04-03 16:11:22 +1100322 struct xfs_mount *mp = bp->b_target->bt_mount;
Carlos Maiolinofb1755a2018-01-24 13:38:48 -0800323 struct xfs_buf_log_item *bip = bp->b_log_item;
Dave Chinner33363fe2013-04-03 16:11:22 +1100324 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800325 xfs_failaddr_t fa;
Dave Chinner33363fe2013-04-03 16:11:22 +1100326
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800327 fa = xfs_dir3_data_verify(bp);
328 if (fa) {
329 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
Dave Chinner33363fe2013-04-03 16:11:22 +1100330 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 Sandeenf1dbcd72014-02-27 15:18:23 +1100339 xfs_buf_update_cksum(bp, XFS_DIR3_DATA_CRC_OFF);
Dave Chinner1813dd62012-11-14 17:54:40 +1100340}
341
Dave Chinner33363fe2013-04-03 16:11:22 +1100342const struct xfs_buf_ops xfs_dir3_data_buf_ops = {
Eric Sandeen233135b2016-01-04 16:10:19 +1100343 .name = "xfs_dir3_data",
Dave Chinner33363fe2013-04-03 16:11:22 +1100344 .verify_read = xfs_dir3_data_read_verify,
345 .verify_write = xfs_dir3_data_write_verify,
Darrick J. Wongb5572592018-01-08 10:51:08 -0800346 .verify_struct = xfs_dir3_data_verify,
Dave Chinner1813dd62012-11-14 17:54:40 +1100347};
348
Dave Chinner33363fe2013-04-03 16:11:22 +1100349static const struct xfs_buf_ops xfs_dir3_data_reada_buf_ops = {
Eric Sandeen233135b2016-01-04 16:10:19 +1100350 .name = "xfs_dir3_data_reada",
Dave Chinner33363fe2013-04-03 16:11:22 +1100351 .verify_read = xfs_dir3_data_reada_verify,
352 .verify_write = xfs_dir3_data_write_verify,
Dave Chinner1813dd62012-11-14 17:54:40 +1100353};
354
Dave Chinner612cfbf2012-11-14 17:52:32 +1100355
Dave Chinnere4813572012-11-12 22:54:14 +1100356int
Dave Chinner33363fe2013-04-03 16:11:22 +1100357xfs_dir3_data_read(
Dave Chinnere4813572012-11-12 22:54:14 +1100358 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 Chinnerd75afeb2013-04-03 16:11:29 +1100364 int err;
365
366 err = xfs_da_read_buf(tp, dp, bno, mapped_bno, bpp,
Dave Chinner33363fe2013-04-03 16:11:22 +1100367 XFS_DATA_FORK, &xfs_dir3_data_buf_ops);
Darrick J. Wong96a3aef2016-12-05 12:37:47 +1100368 if (!err && tp && *bpp)
Dave Chinner61fe1352013-04-03 16:11:30 +1100369 xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_DATA_BUF);
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100370 return err;
Dave Chinnere4813572012-11-12 22:54:14 +1100371}
372
Dave Chinnerda6958c2012-11-12 22:54:18 +1100373int
Dave Chinner33363fe2013-04-03 16:11:22 +1100374xfs_dir3_data_readahead(
Dave Chinnerda6958c2012-11-12 22:54:18 +1100375 struct xfs_inode *dp,
376 xfs_dablk_t bno,
377 xfs_daddr_t mapped_bno)
378{
Eric Sandeen9df2dd02014-04-14 19:01:59 +1000379 return xfs_da_reada_buf(dp, bno, mapped_bno,
Dave Chinner33363fe2013-04-03 16:11:22 +1100380 XFS_DATA_FORK, &xfs_dir3_data_reada_buf_ops);
Dave Chinnerda6958c2012-11-12 22:54:18 +1100381}
382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383/*
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 Chinner2b9ab5a2013-08-12 20:49:37 +1000387xfs_dir2_data_free_t *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388xfs_dir2_data_freefind(
Dave Chinner2ca98772013-10-29 22:11:49 +1100389 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 Torvalds1da177e2005-04-16 15:20:36 -0700392{
393 xfs_dir2_data_free_t *dfp; /* bestfree entry */
394 xfs_dir2_data_aoff_t off; /* offset value needed */
Dave Chinnerb49a0c12013-08-12 20:49:51 +1000395#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 int matched; /* matched the value */
397 int seenzero; /* saw a 0 bestfree entry */
398#endif
399
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200400 off = (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100401
Dave Chinnerb49a0c12013-08-12 20:49:51 +1000402#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 /*
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 Hellwig69ef9212011-07-08 14:36:05 +0200408 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
Dave Chinner33363fe2013-04-03 16:11:22 +1100409 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100410 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 Torvalds1da177e2005-04-16 15:20:36 -0700414 dfp++) {
415 if (!dfp->offset) {
416 ASSERT(!dfp->length);
417 seenzero = 1;
418 continue;
419 }
420 ASSERT(seenzero == 0);
Nathan Scott70e73f52006-03-17 17:26:52 +1100421 if (be16_to_cpu(dfp->offset) == off) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 matched = 1;
Nathan Scottad354eb2006-03-17 17:27:37 +1100423 ASSERT(dfp->length == dup->length);
Nathan Scott70e73f52006-03-17 17:26:52 +1100424 } else if (off < be16_to_cpu(dfp->offset))
Nathan Scottad354eb2006-03-17 17:27:37 +1100425 ASSERT(off + be16_to_cpu(dup->length) <= be16_to_cpu(dfp->offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 else
Nathan Scott70e73f52006-03-17 17:26:52 +1100427 ASSERT(be16_to_cpu(dfp->offset) + be16_to_cpu(dfp->length) <= off);
Nathan Scottad354eb2006-03-17 17:27:37 +1100428 ASSERT(matched || be16_to_cpu(dfp->length) >= be16_to_cpu(dup->length));
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100429 if (dfp > &bf[0])
Nathan Scott70e73f52006-03-17 17:26:52 +1100430 ASSERT(be16_to_cpu(dfp[-1].length) >= be16_to_cpu(dfp[0].length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 }
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 Scottad354eb2006-03-17 17:27:37 +1100437 if (be16_to_cpu(dup->length) <
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100438 be16_to_cpu(bf[XFS_DIR2_DATA_FD_COUNT - 1].length))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 return NULL;
440 /*
441 * Look at the three bestfree entries for our guy.
442 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100443 for (dfp = &bf[0]; dfp < &bf[XFS_DIR2_DATA_FD_COUNT]; dfp++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 if (!dfp->offset)
445 return NULL;
Nathan Scott70e73f52006-03-17 17:26:52 +1100446 if (be16_to_cpu(dfp->offset) == off)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 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 */
458xfs_dir2_data_free_t * /* entry inserted */
459xfs_dir2_data_freeinsert(
Dave Chinner2ca98772013-10-29 22:11:49 +1100460 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 Torvalds1da177e2005-04-16 15:20:36 -0700463 int *loghead) /* log the data header (out) */
464{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 xfs_dir2_data_free_t new; /* new bestfree entry */
466
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200467 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100468 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 Scott70e73f52006-03-17 17:26:52 +1100472 new.length = dup->length;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200473 new.offset = cpu_to_be16((char *)dup - (char *)hdr);
474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 /*
476 * Insert at position 0, 1, or 2; or not at all.
477 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100478 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[0].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 dfp[2] = dfp[1];
480 dfp[1] = dfp[0];
481 dfp[0] = new;
482 *loghead = 1;
483 return &dfp[0];
484 }
Nathan Scott70e73f52006-03-17 17:26:52 +1100485 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[1].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 dfp[2] = dfp[1];
487 dfp[1] = new;
488 *loghead = 1;
489 return &dfp[1];
490 }
Nathan Scott70e73f52006-03-17 17:26:52 +1100491 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[2].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 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 Hellwigba0f32d2005-06-21 15:36:52 +1000502STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503xfs_dir2_data_freeremove(
Dave Chinner2ca98772013-10-29 22:11:49 +1100504 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 Torvalds1da177e2005-04-16 15:20:36 -0700507 int *loghead) /* out: log data header */
508{
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100509
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200510 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100511 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 Torvalds1da177e2005-04-16 15:20:36 -0700515 /*
516 * It's the first entry, slide the next 2 up.
517 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100518 if (dfp == &bf[0]) {
519 bf[0] = bf[1];
520 bf[1] = bf[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 }
522 /*
523 * It's the second entry, slide the 3rd entry up.
524 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100525 else if (dfp == &bf[1])
526 bf[1] = bf[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 /*
528 * Must be the last entry.
529 */
530 else
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100531 ASSERT(dfp == &bf[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 /*
533 * Clear the 3rd entry, must be zero now.
534 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100535 bf[2].length = 0;
536 bf[2].offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 *loghead = 1;
538}
539
540/*
541 * Given a data block, reconstruct its bestfree map.
542 */
543void
Darrick J. Wong523b2e72016-11-08 11:56:51 +1100544xfs_dir2_data_freescan_int(
545 struct xfs_da_geometry *geo,
546 const struct xfs_dir_ops *ops,
Dave Chinner9d23fc82013-10-29 22:11:48 +1100547 struct xfs_dir2_data_hdr *hdr,
548 int *loghead)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 xfs_dir2_data_entry_t *dep; /* active data entry */
551 xfs_dir2_data_unused_t *dup; /* unused data entry */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100552 struct xfs_dir2_data_free *bf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 char *endp; /* end of block's data */
554 char *p; /* current entry pointer */
555
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200556 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
Dave Chinner33363fe2013-04-03 16:11:22 +1100557 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100558 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
559 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
560
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 /*
562 * Start by clearing the table.
563 */
Darrick J. Wong523b2e72016-11-08 11:56:51 +1100564 bf = ops->data_bestfree_p(hdr);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100565 memset(bf, 0, sizeof(*bf) * XFS_DIR2_DATA_FD_COUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 *loghead = 1;
567 /*
568 * Set up pointers.
569 */
Darrick J. Wong523b2e72016-11-08 11:56:51 +1100570 p = (char *)ops->data_entry_p(hdr);
Darrick J. Wongce92d292018-01-16 18:54:12 -0800571 endp = xfs_dir3_data_endp(geo, hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 /*
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 Scottad354eb2006-03-17 17:27:37 +1100580 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200581 ASSERT((char *)dup - (char *)hdr ==
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000582 be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)));
Dave Chinner2ca98772013-10-29 22:11:49 +1100583 xfs_dir2_data_freeinsert(hdr, bf, dup, loghead);
Nathan Scottad354eb2006-03-17 17:27:37 +1100584 p += be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 }
586 /*
587 * For active entries, check their tags and skip them.
588 */
589 else {
590 dep = (xfs_dir2_data_entry_t *)p;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200591 ASSERT((char *)dep - (char *)hdr ==
Darrick J. Wong523b2e72016-11-08 11:56:51 +1100592 be16_to_cpu(*ops->data_entry_tag_p(dep)));
593 p += ops->data_entsize(dep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 }
595 }
596}
597
Darrick J. Wong523b2e72016-11-08 11:56:51 +1100598void
599xfs_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 Torvalds1da177e2005-04-16 15:20:36 -0700608/*
609 * Initialize a data block at the given block number in the directory.
610 * Give back the buffer for the created block.
611 */
612int /* error */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100613xfs_dir3_data_init(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 xfs_da_args_t *args, /* directory operation args */
615 xfs_dir2_db_t blkno, /* logical dir block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000616 struct xfs_buf **bpp) /* output block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000618 struct xfs_buf *bp; /* block buffer */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200619 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 xfs_inode_t *dp; /* incore directory inode */
621 xfs_dir2_data_unused_t *dup; /* unused entry pointer */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100622 struct xfs_dir2_data_free *bf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 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 Chinner2998ab1d2014-06-06 15:07:53 +1000635 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(args->geo, blkno),
636 -1, &bp, XFS_DATA_FORK);
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100637 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 return error;
Dave Chinner33363fe2013-04-03 16:11:22 +1100639 bp->b_ops = &xfs_dir3_data_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100640 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_DATA_BUF);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 /*
643 * Initialize the header.
644 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000645 hdr = bp->b_addr;
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100646 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 Sandeence748ea2015-07-29 11:53:31 +1000653 uuid_copy(&hdr3->uuid, &mp->m_sb.sb_meta_uuid);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100654
655 } else
656 hdr->magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
657
Dave Chinner2ca98772013-10-29 22:11:49 +1100658 bf = dp->d_ops->data_bestfree_p(hdr);
Dave Chinner1c9a5b22013-10-30 09:15:02 +1100659 bf[0].offset = cpu_to_be16(dp->d_ops->data_entry_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 for (i = 1; i < XFS_DIR2_DATA_FD_COUNT; i++) {
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100661 bf[i].length = 0;
662 bf[i].offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 }
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 /*
666 * Set up an unused entry for the block's body.
667 */
Dave Chinner2ca98772013-10-29 22:11:49 +1100668 dup = dp->d_ops->data_unused_p(hdr);
Nathan Scottad354eb2006-03-17 17:27:37 +1100669 dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Dave Chinner8f661932014-06-06 15:15:59 +1000671 t = args->geo->blksize - (uint)dp->d_ops->data_entry_offset;
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100672 bf[0].length = cpu_to_be16(t);
Nathan Scottad354eb2006-03-17 17:27:37 +1100673 dup->length = cpu_to_be16(t);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200674 *xfs_dir2_data_unused_tag_p(dup) = cpu_to_be16((char *)dup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 /*
676 * Log it and return it.
677 */
Dave Chinnerbc851782014-06-06 15:20:54 +1000678 xfs_dir2_data_log_header(args, bp);
679 xfs_dir2_data_log_unused(args, bp, dup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 *bpp = bp;
681 return 0;
682}
683
684/*
685 * Log an active data entry from the block.
686 */
687void
688xfs_dir2_data_log_entry(
Dave Chinnerbc851782014-06-06 15:20:54 +1000689 struct xfs_da_args *args,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000690 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 xfs_dir2_data_entry_t *dep) /* data entry pointer */
692{
Dave Chinner0cb97762013-08-12 20:50:09 +1000693 struct xfs_dir2_data_hdr *hdr = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200695 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
Dave Chinner33363fe2013-04-03 16:11:22 +1100696 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100697 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
698 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200699
Dave Chinnerbc851782014-06-06 15:20:54 +1000700 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 Hellwigc2066e22011-07-08 14:35:38 +0200702 (char *)hdr - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703}
704
705/*
706 * Log a data block header.
707 */
708void
709xfs_dir2_data_log_header(
Dave Chinnerbc851782014-06-06 15:20:54 +1000710 struct xfs_da_args *args,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000711 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
Dave Chinner2ca98772013-10-29 22:11:49 +1100713#ifdef DEBUG
714 struct xfs_dir2_data_hdr *hdr = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200716 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
Dave Chinner33363fe2013-04-03 16:11:22 +1100717 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100718 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
719 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
Dave Chinner2ca98772013-10-29 22:11:49 +1100720#endif
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200721
Dave Chinnerbc851782014-06-06 15:20:54 +1000722 xfs_trans_log_buf(args->trans, bp, 0,
723 args->dp->d_ops->data_entry_offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724}
725
726/*
727 * Log a data unused entry.
728 */
729void
730xfs_dir2_data_log_unused(
Dave Chinnerbc851782014-06-06 15:20:54 +1000731 struct xfs_da_args *args,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000732 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 xfs_dir2_data_unused_t *dup) /* data unused pointer */
734{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000735 xfs_dir2_data_hdr_t *hdr = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200737 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
Dave Chinner33363fe2013-04-03 16:11:22 +1100738 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100739 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
740 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 /*
743 * Log the first part of the unused entry.
744 */
Dave Chinnerbc851782014-06-06 15:20:54 +1000745 xfs_trans_log_buf(args->trans, bp, (uint)((char *)dup - (char *)hdr),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 (uint)((char *)&dup->length + sizeof(dup->length) -
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200747 1 - (char *)hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 /*
749 * Log the end (tag) of the unused entry.
750 */
Dave Chinnerbc851782014-06-06 15:20:54 +1000751 xfs_trans_log_buf(args->trans, bp,
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200752 (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr),
753 (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 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 */
761void
762xfs_dir2_data_make_free(
Dave Chinnerbc851782014-06-06 15:20:54 +1000763 struct xfs_da_args *args,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000764 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 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 Hellwigc2066e22011-07-08 14:35:38 +0200770 xfs_dir2_data_hdr_t *hdr; /* data block pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 xfs_dir2_data_free_t *dfp; /* bestfree pointer */
772 char *endptr; /* end of data area */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 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 Chinnerf5f3d9b2013-04-03 16:11:20 +1100777 struct xfs_dir2_data_free *bf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Dave Chinner1d9025e2012-06-22 18:50:14 +1000779 hdr = bp->b_addr;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200780
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 /*
782 * Figure out where the end of the data area is.
783 */
Darrick J. Wongce92d292018-01-16 18:54:12 -0800784 endptr = xfs_dir3_data_endp(args->geo, hdr);
785 ASSERT(endptr != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 /*
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 Chinnerbc851782014-06-06 15:20:54 +1000791 if (offset > args->dp->d_ops->data_entry_offset) {
Nathan Scott3d693c62006-03-17 17:28:27 +1100792 __be16 *tagp; /* tag just before us */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200794 tagp = (__be16 *)((char *)hdr + offset) - 1;
795 prevdup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
Nathan Scottad354eb2006-03-17 17:27:37 +1100796 if (be16_to_cpu(prevdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 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 Hellwigc2066e22011-07-08 14:35:38 +0200804 if ((char *)hdr + offset + len < endptr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 postdup =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200806 (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
Nathan Scottad354eb2006-03-17 17:27:37 +1100807 if (be16_to_cpu(postdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 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 Chinnerbc851782014-06-06 15:20:54 +1000817 bf = args->dp->d_ops->data_bestfree_p(hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 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 Chinner2ca98772013-10-29 22:11:49 +1100824 dfp = xfs_dir2_data_freefind(hdr, bf, prevdup);
825 dfp2 = xfs_dir2_data_freefind(hdr, bf, postdup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 /*
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 Chinnerf5f3d9b2013-04-03 16:11:20 +1100832 needscan = (bf[2].length != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 /*
834 * Fix up the new big freespace.
835 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800836 be16_add_cpu(&prevdup->length, len + be16_to_cpu(postdup->length));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000837 *xfs_dir2_data_unused_tag_p(prevdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200838 cpu_to_be16((char *)prevdup - (char *)hdr);
Dave Chinnerbc851782014-06-06 15:20:54 +1000839 xfs_dir2_data_log_unused(args, bp, prevdup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 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 Chinnerf5f3d9b2013-04-03 16:11:20 +1100848 if (dfp == &bf[1]) {
849 dfp = &bf[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 ASSERT(dfp2 == dfp);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100851 dfp2 = &bf[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 }
Dave Chinner2ca98772013-10-29 22:11:49 +1100853 xfs_dir2_data_freeremove(hdr, bf, dfp2, needlogp);
854 xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 /*
856 * Now insert the new entry.
857 */
Dave Chinner2ca98772013-10-29 22:11:49 +1100858 dfp = xfs_dir2_data_freeinsert(hdr, bf, prevdup,
859 needlogp);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100860 ASSERT(dfp == &bf[0]);
Nathan Scottad354eb2006-03-17 17:27:37 +1100861 ASSERT(dfp->length == prevdup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 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 Chinner2ca98772013-10-29 22:11:49 +1100870 dfp = xfs_dir2_data_freefind(hdr, bf, prevdup);
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800871 be16_add_cpu(&prevdup->length, len);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000872 *xfs_dir2_data_unused_tag_p(prevdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200873 cpu_to_be16((char *)prevdup - (char *)hdr);
Dave Chinnerbc851782014-06-06 15:20:54 +1000874 xfs_dir2_data_log_unused(args, bp, prevdup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 /*
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 Chinner2ca98772013-10-29 22:11:49 +1100881 xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp);
882 xfs_dir2_data_freeinsert(hdr, bf, prevdup, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 }
884 /*
885 * Otherwise we need a scan if the new entry is big enough.
886 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100887 else {
Nathan Scottad354eb2006-03-17 17:27:37 +1100888 needscan = be16_to_cpu(prevdup->length) >
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100889 be16_to_cpu(bf[2].length);
Nathan Scott70e73f52006-03-17 17:26:52 +1100890 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 }
892 /*
893 * The following entry is free, merge with it.
894 */
895 else if (postdup) {
Dave Chinner2ca98772013-10-29 22:11:49 +1100896 dfp = xfs_dir2_data_freefind(hdr, bf, postdup);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200897 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
Nathan Scottad354eb2006-03-17 17:27:37 +1100898 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
899 newdup->length = cpu_to_be16(len + be16_to_cpu(postdup->length));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000900 *xfs_dir2_data_unused_tag_p(newdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200901 cpu_to_be16((char *)newdup - (char *)hdr);
Dave Chinnerbc851782014-06-06 15:20:54 +1000902 xfs_dir2_data_log_unused(args, bp, newdup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 /*
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 Chinner2ca98772013-10-29 22:11:49 +1100909 xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp);
910 xfs_dir2_data_freeinsert(hdr, bf, newdup, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 }
912 /*
913 * Otherwise we need a scan if the new entry is big enough.
914 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100915 else {
Nathan Scottad354eb2006-03-17 17:27:37 +1100916 needscan = be16_to_cpu(newdup->length) >
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100917 be16_to_cpu(bf[2].length);
Nathan Scott70e73f52006-03-17 17:26:52 +1100918 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 }
920 /*
921 * Neither neighbor is free. Make a new entry.
922 */
923 else {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200924 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
Nathan Scottad354eb2006-03-17 17:27:37 +1100925 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
926 newdup->length = cpu_to_be16(len);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000927 *xfs_dir2_data_unused_tag_p(newdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200928 cpu_to_be16((char *)newdup - (char *)hdr);
Dave Chinnerbc851782014-06-06 15:20:54 +1000929 xfs_dir2_data_log_unused(args, bp, newdup);
Dave Chinner2ca98772013-10-29 22:11:49 +1100930 xfs_dir2_data_freeinsert(hdr, bf, newdup, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 }
932 *needscanp = needscan;
933}
934
Darrick J. Wong6915ef32018-03-23 10:06:51 -0700935/* Check our free data for obvious signs of corruption. */
936static inline xfs_failaddr_t
937xfs_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. */
961static inline xfs_failaddr_t
962xfs_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 Torvalds1da177e2005-04-16 15:20:36 -0700976/*
977 * Take a byte range out of an existing unused space and make it un-free.
978 */
Darrick J. Wong6915ef32018-03-23 10:06:51 -0700979int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980xfs_dir2_data_use_free(
Dave Chinnerbc851782014-06-06 15:20:54 +1000981 struct xfs_da_args *args,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000982 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 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 Hellwigc2066e22011-07-08 14:35:38 +0200989 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 xfs_dir2_data_free_t *dfp; /* bestfree pointer */
Darrick J. Wong6915ef32018-03-23 10:06:51 -0700991 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 Torvalds1da177e2005-04-16 15:20:36 -0700995 int matchback; /* matches end of freespace */
996 int matchfront; /* matches start of freespace */
997 int needscan; /* need to regen bestfree */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 int oldlen; /* old unused entry's length */
999
Dave Chinner1d9025e2012-06-22 18:50:14 +10001000 hdr = bp->b_addr;
Darrick J. Wong6915ef32018-03-23 10:06:51 -07001001 fa = xfs_dir2_data_check_free(hdr, dup, offset, len);
1002 if (fa)
1003 goto corrupt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 /*
1005 * Look up the entry in the bestfree table.
1006 */
Nathan Scottad354eb2006-03-17 17:27:37 +11001007 oldlen = be16_to_cpu(dup->length);
Dave Chinnerbc851782014-06-06 15:20:54 +10001008 bf = args->dp->d_ops->data_bestfree_p(hdr);
Dave Chinner2ca98772013-10-29 22:11:49 +11001009 dfp = xfs_dir2_data_freefind(hdr, bf, dup);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +11001010 ASSERT(dfp || oldlen <= be16_to_cpu(bf[2].length));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 /*
1012 * Check for alignment with front and back of the entry.
1013 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001014 matchfront = (char *)dup - (char *)hdr == offset;
1015 matchback = (char *)dup + oldlen - (char *)hdr == offset + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 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 Chinnerf5f3d9b2013-04-03 16:11:20 +11001024 needscan = (bf[2].offset != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 if (!needscan)
Dave Chinner2ca98772013-10-29 22:11:49 +11001026 xfs_dir2_data_freeremove(hdr, bf, dfp,
1027 needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 }
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 Hellwigc2066e22011-07-08 14:35:38 +02001035 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
Nathan Scottad354eb2006-03-17 17:27:37 +11001036 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
1037 newdup->length = cpu_to_be16(oldlen - len);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001038 *xfs_dir2_data_unused_tag_p(newdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001039 cpu_to_be16((char *)newdup - (char *)hdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001040 xfs_dir2_data_log_unused(args, bp, newdup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 /*
1042 * If it was in the table, remove it and add the new one.
1043 */
1044 if (dfp) {
Dave Chinner2ca98772013-10-29 22:11:49 +11001045 xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp);
1046 dfp = xfs_dir2_data_freeinsert(hdr, bf, newdup,
1047 needlogp);
Darrick J. Wong6915ef32018-03-23 10:06:51 -07001048 fa = xfs_dir2_data_check_new_free(hdr, dfp, newdup);
1049 if (fa)
1050 goto corrupt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 /*
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 Chinnerf5f3d9b2013-04-03 16:11:20 +11001056 needscan = dfp == &bf[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 }
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 Hellwigc2066e22011-07-08 14:35:38 +02001065 newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001066 *xfs_dir2_data_unused_tag_p(newdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001067 cpu_to_be16((char *)newdup - (char *)hdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001068 xfs_dir2_data_log_unused(args, bp, newdup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 /*
1070 * If it was in the table, remove it and add the new one.
1071 */
1072 if (dfp) {
Dave Chinner2ca98772013-10-29 22:11:49 +11001073 xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp);
1074 dfp = xfs_dir2_data_freeinsert(hdr, bf, newdup,
1075 needlogp);
Darrick J. Wong6915ef32018-03-23 10:06:51 -07001076 fa = xfs_dir2_data_check_new_free(hdr, dfp, newdup);
1077 if (fa)
1078 goto corrupt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 /*
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 Chinnerf5f3d9b2013-04-03 16:11:20 +11001084 needscan = dfp == &bf[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 }
1086 }
1087 /*
1088 * Poking out the middle of an entry.
1089 * Make two new entries.
1090 */
1091 else {
1092 newdup = dup;
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001093 newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001094 *xfs_dir2_data_unused_tag_p(newdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001095 cpu_to_be16((char *)newdup - (char *)hdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001096 xfs_dir2_data_log_unused(args, bp, newdup);
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001097 newdup2 = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
Nathan Scottad354eb2006-03-17 17:27:37 +11001098 newdup2->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
1099 newdup2->length = cpu_to_be16(oldlen - len - be16_to_cpu(newdup->length));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001100 *xfs_dir2_data_unused_tag_p(newdup2) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001101 cpu_to_be16((char *)newdup2 - (char *)hdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001102 xfs_dir2_data_log_unused(args, bp, newdup2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 /*
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 Chinnerf5f3d9b2013-04-03 16:11:20 +11001112 needscan = (bf[2].length != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 if (!needscan) {
Dave Chinner2ca98772013-10-29 22:11:49 +11001114 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 Hellwigc2066e22011-07-08 14:35:38 +02001119 needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 }
1121 }
1122 }
1123 *needscanp = needscan;
Darrick J. Wong6915ef32018-03-23 10:06:51 -07001124 return 0;
1125corrupt:
1126 xfs_corruption_error(__func__, XFS_ERRLEVEL_LOW, args->dp->i_mount,
1127 hdr, __FILE__, __LINE__, fa);
1128 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129}
Darrick J. Wongce92d292018-01-16 18:54:12 -08001130
1131/* Find the end of the entry data in a data/block format dir block. */
1132void *
1133xfs_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}