blob: c90180f2ba5c4db4034c0115f1b1eb3a4ac482fb [file] [log] [blame]
Dave Chinner0b61f8a2018-06-05 19:42:14 -07001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Nathan Scott7b718762005-11-02 14:58:39 +11003 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +11004 * Copyright (c) 2013 Red Hat, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11005 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +11008#include "xfs_fs.h"
Darrick J. Wong5467b342019-06-28 19:25:35 -07009#include "xfs_shared.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110010#include "xfs_format.h"
Dave Chinner239880e2013-10-23 10:50:10 +110011#include "xfs_log_format.h"
12#include "xfs_trans_resv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include "xfs_inode.h"
Dave Chinner2b9ab5a2013-08-12 20:49:37 +100015#include "xfs_dir2.h"
Christoph Hellwigfdbb8c52019-11-08 15:05:37 -080016#include "xfs_dir2_priv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include "xfs_error.h"
Dave Chinner239880e2013-10-23 10:50:10 +110018#include "xfs_trans.h"
Dave Chinner33363fe2013-04-03 16:11:22 +110019#include "xfs_buf_item.h"
Brian Fostera45086e2015-10-12 15:59:25 +110020#include "xfs_log.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Darrick J. Wonge4f45ef2018-06-03 16:10:13 -070022static 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 Hellwig1848b602019-11-08 15:05:39 -080027struct xfs_dir2_data_free *
28xfs_dir2_data_bestfree_p(
29 struct xfs_mount *mp,
30 struct xfs_dir2_data_hdr *hdr)
31{
Dave Chinnerebd90272021-08-18 18:46:55 -070032 if (xfs_has_crc(mp))
Christoph Hellwig1848b602019-11-08 15:05:39 -080033 return ((struct xfs_dir3_data_hdr *)hdr)->best_free;
34 return hdr->bestfree;
35}
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037/*
Christoph Hellwig7e8ae7bd2019-11-08 15:05:37 -080038 * Pointer to an entry's tag word.
39 */
40__be16 *
41xfs_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 Hellwig59b8b462019-11-08 15:05:48 -080049uint8_t
50xfs_dir2_data_get_ftype(
51 struct xfs_mount *mp,
52 struct xfs_dir2_data_entry *dep)
53{
Dave Chinnerebd90272021-08-18 18:46:55 -070054 if (xfs_has_ftype(mp)) {
Christoph Hellwig59b8b462019-11-08 15:05:48 -080055 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
64void
65xfs_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 Chinnerebd90272021-08-18 18:46:55 -070073 if (xfs_has_ftype(mp))
Christoph Hellwig59b8b462019-11-08 15:05:48 -080074 dep->name[dep->namelen] = ftype;
75}
76
Christoph Hellwig7e8ae7bd2019-11-08 15:05:37 -080077/*
Christoph Hellwig48a71392019-11-08 15:05:35 -080078 * 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 */
83static inline unsigned int
84xfs_dir2_data_max_leaf_entries(
Christoph Hellwig48a71392019-11-08 15:05:35 -080085 struct xfs_da_geometry *geo)
86{
87 return (geo->blksize - sizeof(struct xfs_dir2_block_tail) -
Christoph Hellwigd73e1ce2019-11-08 15:05:38 -080088 geo->data_entry_offset) /
Christoph Hellwig48a71392019-11-08 15:05:35 -080089 sizeof(struct xfs_dir2_leaf_entry);
90}
91
92/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 * Check the consistency of the data block.
94 * The input can also be a block-format directory.
Darrick J. Wonga6a781a2018-01-08 10:51:03 -080095 * Return NULL if the buffer is good, otherwise the address of the error.
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 */
Darrick J. Wonga6a781a2018-01-08 10:51:03 -080097xfs_failaddr_t
Dave Chinner33363fe2013-04-03 16:11:22 +110098__xfs_dir3_data_check(
Dave Chinner1d9025e2012-06-22 18:50:14 +100099 struct xfs_inode *dp, /* incore inode pointer */
100 struct xfs_buf *bp) /* data block's buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
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 Hellwigc2066e22011-07-08 14:35:38 +0200106 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 xfs_dir2_data_free_t *dfp; /* bestfree entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 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 Hellwigdbd329f12019-06-28 19:27:29 -0700113 struct xfs_mount *mp = bp->b_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 int stale; /* count of stale leaves */
Barry Naujok5163f952008-05-21 16:41:01 +1000115 struct xfs_name name;
Christoph Hellwig48a71392019-11-08 15:05:35 -0800116 unsigned int offset;
117 unsigned int end;
Christoph Hellwig48a71392019-11-08 15:05:35 -0800118 struct xfs_da_geometry *geo = mp->m_dir_geo;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200119
Dave Chinner9d23fc82013-10-29 22:11:48 +1100120 /*
Christoph Hellwig59b8b462019-11-08 15:05:48 -0800121 * If this isn't a directory, something is seriously wrong. Bail out.
Dave Chinner9d23fc82013-10-29 22:11:48 +1100122 */
Christoph Hellwig59b8b462019-11-08 15:05:48 -0800123 if (dp && !S_ISDIR(VFS_I(dp)->i_mode))
Darrick J. Wong46c59732018-01-09 11:11:42 -0800124 return __this_address;
125
Dave Chinnera6293622013-10-29 22:11:56 +1100126 hdr = bp->b_addr;
Christoph Hellwigd73e1ce2019-11-08 15:05:38 -0800127 offset = geo->data_entry_offset;
Dave Chinnera6293622013-10-29 22:11:56 +1100128
Dave Chinner82025d72012-11-12 22:54:12 +1100129 switch (hdr->magic) {
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100130 case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC):
Dave Chinner82025d72012-11-12 22:54:12 +1100131 case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC):
Dave Chinner8f661932014-06-06 15:15:59 +1000132 btp = xfs_dir2_block_tail_p(geo, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000133 lep = xfs_dir2_block_leaf_p(btp);
Dave Chinnera6293622013-10-29 22:11:56 +1100134
Darrick J. Wong9101d372018-01-08 10:51:01 -0800135 if (be32_to_cpu(btp->count) >=
Christoph Hellwigd73e1ce2019-11-08 15:05:38 -0800136 xfs_dir2_data_max_leaf_entries(geo))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800137 return __this_address;
Dave Chinner82025d72012-11-12 22:54:12 +1100138 break;
Dave Chinner33363fe2013-04-03 16:11:22 +1100139 case cpu_to_be32(XFS_DIR3_DATA_MAGIC):
Dave Chinner82025d72012-11-12 22:54:12 +1100140 case cpu_to_be32(XFS_DIR2_DATA_MAGIC):
Dave Chinner82025d72012-11-12 22:54:12 +1100141 break;
142 default:
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800143 return __this_address;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200144 }
Christoph Hellwig5c072122019-11-08 15:05:36 -0800145 end = xfs_dir3_data_end_offset(geo, hdr);
146 if (!end)
Darrick J. Wongce92d292018-01-16 18:54:12 -0800147 return __this_address;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 /*
150 * Account for zero bestfree entries.
151 */
Christoph Hellwig1848b602019-11-08 15:05:39 -0800152 bf = xfs_dir2_data_bestfree_p(mp, hdr);
Dave Chinnera6293622013-10-29 22:11:56 +1100153 count = lastfree = freeseen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 if (!bf[0].length) {
Darrick J. Wong9101d372018-01-08 10:51:01 -0800155 if (bf[0].offset)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800156 return __this_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 freeseen |= 1 << 0;
158 }
159 if (!bf[1].length) {
Darrick J. Wong9101d372018-01-08 10:51:01 -0800160 if (bf[1].offset)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800161 return __this_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 freeseen |= 1 << 1;
163 }
164 if (!bf[2].length) {
Darrick J. Wong9101d372018-01-08 10:51:01 -0800165 if (bf[2].offset)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800166 return __this_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 freeseen |= 1 << 2;
168 }
Dave Chinner82025d72012-11-12 22:54:12 +1100169
Darrick J. Wong9101d372018-01-08 10:51:01 -0800170 if (be16_to_cpu(bf[0].length) < be16_to_cpu(bf[1].length))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800171 return __this_address;
Darrick J. Wong9101d372018-01-08 10:51:01 -0800172 if (be16_to_cpu(bf[1].length) < be16_to_cpu(bf[2].length))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800173 return __this_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 /*
175 * Loop over the data/unused entries.
176 */
Christoph Hellwig48a71392019-11-08 15:05:35 -0800177 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 Torvalds1da177e2005-04-16 15:20:36 -0700181 /*
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 Scottad354eb2006-03-17 17:27:37 +1100186 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
Darrick J. Wonge4f45ef2018-06-03 16:10:13 -0700187 xfs_failaddr_t fa;
188
Darrick J. Wong9101d372018-01-08 10:51:01 -0800189 if (lastfree != 0)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800190 return __this_address;
Christoph Hellwig48a71392019-11-08 15:05:35 -0800191 if (offset + be16_to_cpu(dup->length) > end)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800192 return __this_address;
Darrick J. Wong9101d372018-01-08 10:51:01 -0800193 if (be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)) !=
Christoph Hellwig48a71392019-11-08 15:05:35 -0800194 offset)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800195 return __this_address;
Darrick J. Wonge4f45ef2018-06-03 16:10:13 -0700196 fa = xfs_dir2_data_freefind_verify(hdr, bf, dup, &dfp);
197 if (fa)
198 return fa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 if (dfp) {
200 i = (int)(dfp - bf);
Darrick J. Wong9101d372018-01-08 10:51:01 -0800201 if ((freeseen & (1 << i)) != 0)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800202 return __this_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 freeseen |= 1 << i;
Nathan Scott70e73f52006-03-17 17:26:52 +1100204 } else {
Darrick J. Wong9101d372018-01-08 10:51:01 -0800205 if (be16_to_cpu(dup->length) >
206 be16_to_cpu(bf[2].length))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800207 return __this_address;
Nathan Scott70e73f52006-03-17 17:26:52 +1100208 }
Christoph Hellwig48a71392019-11-08 15:05:35 -0800209 offset += be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 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. Wong9101d372018-01-08 10:51:01 -0800219 if (dep->namelen == 0)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800220 return __this_address;
Dave Chinner39d3c0b2021-03-22 09:52:05 -0700221 if (!xfs_verify_dir_ino(mp, be64_to_cpu(dep->inumber)))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800222 return __this_address;
Christoph Hellwigfdbb8c52019-11-08 15:05:37 -0800223 if (offset + xfs_dir2_data_entsize(mp, dep->namelen) > end)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800224 return __this_address;
Christoph Hellwig7e8ae7bd2019-11-08 15:05:37 -0800225 if (be16_to_cpu(*xfs_dir2_data_entry_tag_p(mp, dep)) != offset)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800226 return __this_address;
Christoph Hellwig59b8b462019-11-08 15:05:48 -0800227 if (xfs_dir2_data_get_ftype(mp, dep) >= XFS_DIR3_FT_MAX)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800228 return __this_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 count++;
230 lastfree = 0;
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100231 if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
232 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) {
Dave Chinner7dda6e82014-06-06 15:11:18 +1000233 addr = xfs_dir2_db_off_to_dataptr(geo, geo->datablk,
234 (xfs_dir2_data_aoff_t)
235 ((char *)dep - (char *)hdr));
Barry Naujok5163f952008-05-21 16:41:01 +1000236 name.name = dep->name;
237 name.len = dep->namelen;
Christoph Hellwigd8d11fc2019-11-11 12:59:26 -0800238 hash = xfs_dir2_hashname(mp, &name);
Nathan Scotte922fff2006-03-17 17:27:56 +1100239 for (i = 0; i < be32_to_cpu(btp->count); i++) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100240 if (be32_to_cpu(lep[i].address) == addr &&
241 be32_to_cpu(lep[i].hashval) == hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 break;
243 }
Darrick J. Wong9101d372018-01-08 10:51:01 -0800244 if (i >= be32_to_cpu(btp->count))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800245 return __this_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 }
Christoph Hellwigfdbb8c52019-11-08 15:05:37 -0800247 offset += xfs_dir2_data_entsize(mp, dep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 }
249 /*
250 * Need to have seen all the entries and all the bestfree slots.
251 */
Darrick J. Wong9101d372018-01-08 10:51:01 -0800252 if (freeseen != 7)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800253 return __this_address;
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100254 if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
255 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) {
Nathan Scotte922fff2006-03-17 17:27:56 +1100256 for (i = stale = 0; i < be32_to_cpu(btp->count); i++) {
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200257 if (lep[i].address ==
258 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 stale++;
Darrick J. Wong9101d372018-01-08 10:51:01 -0800260 if (i > 0 && be32_to_cpu(lep[i].hashval) <
261 be32_to_cpu(lep[i - 1].hashval))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800262 return __this_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 }
Darrick J. Wong9101d372018-01-08 10:51:01 -0800264 if (count != be32_to_cpu(btp->count) - be32_to_cpu(btp->stale))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800265 return __this_address;
Darrick J. Wong9101d372018-01-08 10:51:01 -0800266 if (stale != be32_to_cpu(btp->stale))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800267 return __this_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 }
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800269 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800272#ifdef DEBUG
273void
274xfs_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. Wong2551a532018-06-04 10:23:54 -0700284 bp->b_addr, BBTOB(bp->b_length), __FILE__, __LINE__,
285 fa);
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800286 ASSERT(0);
287}
288#endif
289
290static xfs_failaddr_t
Dave Chinner33363fe2013-04-03 16:11:22 +1100291xfs_dir3_data_verify(
Dave Chinnere4813572012-11-12 22:54:14 +1100292 struct xfs_buf *bp)
293{
Christoph Hellwigdbd329f12019-06-28 19:27:29 -0700294 struct xfs_mount *mp = bp->b_mount;
Dave Chinner33363fe2013-04-03 16:11:22 +1100295 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
Dave Chinnere4813572012-11-12 22:54:14 +1100296
Brian Foster39708c22019-02-07 10:45:48 -0800297 if (!xfs_verify_magic(bp, hdr3->magic))
298 return __this_address;
299
Dave Chinnerebd90272021-08-18 18:46:55 -0700300 if (xfs_has_crc(mp)) {
Eric Sandeence748ea2015-07-29 11:53:31 +1000301 if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_meta_uuid))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800302 return __this_address;
Dave Chinner33363fe2013-04-03 16:11:22 +1100303 if (be64_to_cpu(hdr3->blkno) != bp->b_bn)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800304 return __this_address;
Brian Fostera45086e2015-10-12 15:59:25 +1100305 if (!xfs_log_check_lsn(mp, be64_to_cpu(hdr3->lsn)))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800306 return __this_address;
Dave Chinnere4813572012-11-12 22:54:14 +1100307 }
Darrick J. Wong9101d372018-01-08 10:51:01 -0800308 return __xfs_dir3_data_check(NULL, bp);
Dave Chinner612cfbf2012-11-14 17:52:32 +1100309}
Dave Chinnere4813572012-11-12 22:54:14 +1100310
Dave Chinner1813dd62012-11-14 17:54:40 +1100311/*
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 */
316static void
Dave Chinner33363fe2013-04-03 16:11:22 +1100317xfs_dir3_data_reada_verify(
Dave Chinner1813dd62012-11-14 17:54:40 +1100318 struct xfs_buf *bp)
Dave Chinner612cfbf2012-11-14 17:52:32 +1100319{
Dave Chinner1813dd62012-11-14 17:54:40 +1100320 struct xfs_dir2_data_hdr *hdr = bp->b_addr;
321
322 switch (hdr->magic) {
323 case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC):
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100324 case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC):
325 bp->b_ops = &xfs_dir3_block_buf_ops;
Dave Chinner1813dd62012-11-14 17:54:40 +1100326 bp->b_ops->verify_read(bp);
327 return;
328 case cpu_to_be32(XFS_DIR2_DATA_MAGIC):
Dave Chinner33363fe2013-04-03 16:11:22 +1100329 case cpu_to_be32(XFS_DIR3_DATA_MAGIC):
Darrick J. Wong2f123bc2015-08-19 10:33:58 +1000330 bp->b_ops = &xfs_dir3_data_buf_ops;
331 bp->b_ops->verify_read(bp);
Dave Chinner1813dd62012-11-14 17:54:40 +1100332 return;
333 default:
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800334 xfs_verifier_error(bp, -EFSCORRUPTED, __this_address);
Dave Chinner1813dd62012-11-14 17:54:40 +1100335 break;
336 }
Dave Chinner612cfbf2012-11-14 17:52:32 +1100337}
338
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100339static void
Dave Chinner33363fe2013-04-03 16:11:22 +1100340xfs_dir3_data_read_verify(
Dave Chinner612cfbf2012-11-14 17:52:32 +1100341 struct xfs_buf *bp)
342{
Christoph Hellwigdbd329f12019-06-28 19:27:29 -0700343 struct xfs_mount *mp = bp->b_mount;
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800344 xfs_failaddr_t fa;
Dave Chinner33363fe2013-04-03 16:11:22 +1100345
Dave Chinner38c26bf2021-08-18 18:46:37 -0700346 if (xfs_has_crc(mp) &&
Darrick J. Wong31ca03c2018-01-08 10:51:02 -0800347 !xfs_buf_verify_cksum(bp, XFS_DIR3_DATA_CRC_OFF))
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800348 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 Chinnere4813572012-11-12 22:54:14 +1100354}
355
Dave Chinner1813dd62012-11-14 17:54:40 +1100356static void
Dave Chinner33363fe2013-04-03 16:11:22 +1100357xfs_dir3_data_write_verify(
Dave Chinner1813dd62012-11-14 17:54:40 +1100358 struct xfs_buf *bp)
359{
Christoph Hellwigdbd329f12019-06-28 19:27:29 -0700360 struct xfs_mount *mp = bp->b_mount;
Carlos Maiolinofb1755a2018-01-24 13:38:48 -0800361 struct xfs_buf_log_item *bip = bp->b_log_item;
Dave Chinner33363fe2013-04-03 16:11:22 +1100362 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800363 xfs_failaddr_t fa;
Dave Chinner33363fe2013-04-03 16:11:22 +1100364
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800365 fa = xfs_dir3_data_verify(bp);
366 if (fa) {
367 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
Dave Chinner33363fe2013-04-03 16:11:22 +1100368 return;
369 }
370
Dave Chinner38c26bf2021-08-18 18:46:37 -0700371 if (!xfs_has_crc(mp))
Dave Chinner33363fe2013-04-03 16:11:22 +1100372 return;
373
374 if (bip)
375 hdr3->lsn = cpu_to_be64(bip->bli_item.li_lsn);
376
Eric Sandeenf1dbcd72014-02-27 15:18:23 +1100377 xfs_buf_update_cksum(bp, XFS_DIR3_DATA_CRC_OFF);
Dave Chinner1813dd62012-11-14 17:54:40 +1100378}
379
Dave Chinner33363fe2013-04-03 16:11:22 +1100380const struct xfs_buf_ops xfs_dir3_data_buf_ops = {
Eric Sandeen233135b2016-01-04 16:10:19 +1100381 .name = "xfs_dir3_data",
Brian Foster39708c22019-02-07 10:45:48 -0800382 .magic = { cpu_to_be32(XFS_DIR2_DATA_MAGIC),
383 cpu_to_be32(XFS_DIR3_DATA_MAGIC) },
Dave Chinner33363fe2013-04-03 16:11:22 +1100384 .verify_read = xfs_dir3_data_read_verify,
385 .verify_write = xfs_dir3_data_write_verify,
Darrick J. Wongb5572592018-01-08 10:51:08 -0800386 .verify_struct = xfs_dir3_data_verify,
Dave Chinner1813dd62012-11-14 17:54:40 +1100387};
388
Dave Chinner33363fe2013-04-03 16:11:22 +1100389static const struct xfs_buf_ops xfs_dir3_data_reada_buf_ops = {
Eric Sandeen233135b2016-01-04 16:10:19 +1100390 .name = "xfs_dir3_data_reada",
Brian Foster39708c22019-02-07 10:45:48 -0800391 .magic = { cpu_to_be32(XFS_DIR2_DATA_MAGIC),
392 cpu_to_be32(XFS_DIR3_DATA_MAGIC) },
Dave Chinner33363fe2013-04-03 16:11:22 +1100393 .verify_read = xfs_dir3_data_reada_verify,
394 .verify_write = xfs_dir3_data_write_verify,
Dave Chinner1813dd62012-11-14 17:54:40 +1100395};
396
Darrick J. Wonga10c21e2020-03-11 10:37:56 -0700397static xfs_failaddr_t
398xfs_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 Chinnerebd90272021-08-18 18:46:55 -0700404 if (xfs_has_crc(mp)) {
Darrick J. Wonga10c21e2020-03-11 10:37:56 -0700405 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 Chinner612cfbf2012-11-14 17:52:32 +1100413
Dave Chinnere4813572012-11-12 22:54:14 +1100414int
Dave Chinner33363fe2013-04-03 16:11:22 +1100415xfs_dir3_data_read(
Dave Chinnere4813572012-11-12 22:54:14 +1100416 struct xfs_trans *tp,
417 struct xfs_inode *dp,
418 xfs_dablk_t bno,
Christoph Hellwigcd2c9f12019-11-20 09:46:04 -0800419 unsigned int flags,
Dave Chinnere4813572012-11-12 22:54:14 +1100420 struct xfs_buf **bpp)
421{
Darrick J. Wonga10c21e2020-03-11 10:37:56 -0700422 xfs_failaddr_t fa;
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100423 int err;
424
Christoph Hellwigcd2c9f12019-11-20 09:46:04 -0800425 err = xfs_da_read_buf(tp, dp, bno, flags, bpp, XFS_DATA_FORK,
426 &xfs_dir3_data_buf_ops);
Darrick J. Wonga10c21e2020-03-11 10:37:56 -0700427 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 Chinnerd75afeb2013-04-03 16:11:29 +1100440 return err;
Dave Chinnere4813572012-11-12 22:54:14 +1100441}
442
Dave Chinnerda6958c2012-11-12 22:54:18 +1100443int
Dave Chinner33363fe2013-04-03 16:11:22 +1100444xfs_dir3_data_readahead(
Dave Chinnerda6958c2012-11-12 22:54:18 +1100445 struct xfs_inode *dp,
446 xfs_dablk_t bno,
Christoph Hellwig06566fd2019-11-20 09:46:02 -0800447 unsigned int flags)
Dave Chinnerda6958c2012-11-12 22:54:18 +1100448{
Christoph Hellwig06566fd2019-11-20 09:46:02 -0800449 return xfs_da_reada_buf(dp, bno, flags, XFS_DATA_FORK,
450 &xfs_dir3_data_reada_buf_ops);
Dave Chinnerda6958c2012-11-12 22:54:18 +1100451}
452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453/*
Darrick J. Wonge4f45ef2018-06-03 16:10:13 -0700454 * Find the bestfree entry that exactly coincides with unused directory space
455 * or a verifier error because the bestfree data are bad.
456 */
457static xfs_failaddr_t
458xfs_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 Torvalds1da177e2005-04-16 15:20:36 -0700513 * Given a data block and an unused entry from that block,
514 * return the bestfree entry if any that corresponds to it.
515 */
Dave Chinner2b9ab5a2013-08-12 20:49:37 +1000516xfs_dir2_data_free_t *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517xfs_dir2_data_freefind(
Dave Chinner2ca98772013-10-29 22:11:49 +1100518 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 Torvalds1da177e2005-04-16 15:20:36 -0700521{
522 xfs_dir2_data_free_t *dfp; /* bestfree entry */
523 xfs_dir2_data_aoff_t off; /* offset value needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200525 off = (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 /*
528 * If this is smaller than the smallest bestfree entry,
529 * it can't be there since they're sorted.
530 */
Nathan Scottad354eb2006-03-17 17:27:37 +1100531 if (be16_to_cpu(dup->length) <
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100532 be16_to_cpu(bf[XFS_DIR2_DATA_FD_COUNT - 1].length))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 return NULL;
534 /*
535 * Look at the three bestfree entries for our guy.
536 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100537 for (dfp = &bf[0]; dfp < &bf[XFS_DIR2_DATA_FD_COUNT]; dfp++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 if (!dfp->offset)
539 return NULL;
Nathan Scott70e73f52006-03-17 17:26:52 +1100540 if (be16_to_cpu(dfp->offset) == off)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 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 */
552xfs_dir2_data_free_t * /* entry inserted */
553xfs_dir2_data_freeinsert(
Dave Chinner2ca98772013-10-29 22:11:49 +1100554 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 Torvalds1da177e2005-04-16 15:20:36 -0700557 int *loghead) /* log the data header (out) */
558{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 xfs_dir2_data_free_t new; /* new bestfree entry */
560
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200561 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100562 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 Scott70e73f52006-03-17 17:26:52 +1100566 new.length = dup->length;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200567 new.offset = cpu_to_be16((char *)dup - (char *)hdr);
568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 /*
570 * Insert at position 0, 1, or 2; or not at all.
571 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100572 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[0].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 dfp[2] = dfp[1];
574 dfp[1] = dfp[0];
575 dfp[0] = new;
576 *loghead = 1;
577 return &dfp[0];
578 }
Nathan Scott70e73f52006-03-17 17:26:52 +1100579 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[1].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 dfp[2] = dfp[1];
581 dfp[1] = new;
582 *loghead = 1;
583 return &dfp[1];
584 }
Nathan Scott70e73f52006-03-17 17:26:52 +1100585 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[2].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 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 Hellwigba0f32d2005-06-21 15:36:52 +1000596STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597xfs_dir2_data_freeremove(
Dave Chinner2ca98772013-10-29 22:11:49 +1100598 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 Torvalds1da177e2005-04-16 15:20:36 -0700601 int *loghead) /* out: log data header */
602{
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100603
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200604 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100605 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 Torvalds1da177e2005-04-16 15:20:36 -0700609 /*
610 * It's the first entry, slide the next 2 up.
611 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100612 if (dfp == &bf[0]) {
613 bf[0] = bf[1];
614 bf[1] = bf[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 }
616 /*
617 * It's the second entry, slide the 3rd entry up.
618 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100619 else if (dfp == &bf[1])
620 bf[1] = bf[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 /*
622 * Must be the last entry.
623 */
624 else
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100625 ASSERT(dfp == &bf[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 /*
627 * Clear the 3rd entry, must be zero now.
628 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100629 bf[2].length = 0;
630 bf[2].offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 *loghead = 1;
632}
633
634/*
635 * Given a data block, reconstruct its bestfree map.
636 */
637void
Christoph Hellwigae429762019-11-08 15:06:02 -0800638xfs_dir2_data_freescan(
Christoph Hellwigfdbb8c52019-11-08 15:05:37 -0800639 struct xfs_mount *mp,
Christoph Hellwig62479f52019-11-08 15:05:34 -0800640 struct xfs_dir2_data_hdr *hdr,
641 int *loghead)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
Christoph Hellwigd73e1ce2019-11-08 15:05:38 -0800643 struct xfs_da_geometry *geo = mp->m_dir_geo;
Christoph Hellwig1848b602019-11-08 15:05:39 -0800644 struct xfs_dir2_data_free *bf = xfs_dir2_data_bestfree_p(mp, hdr);
Christoph Hellwig62479f52019-11-08 15:05:34 -0800645 void *addr = hdr;
Christoph Hellwigd73e1ce2019-11-08 15:05:38 -0800646 unsigned int offset = geo->data_entry_offset;
Christoph Hellwig62479f52019-11-08 15:05:34 -0800647 unsigned int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200649 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
Dave Chinner33363fe2013-04-03 16:11:22 +1100650 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100651 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
652 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 /*
655 * Start by clearing the table.
656 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100657 memset(bf, 0, sizeof(*bf) * XFS_DIR2_DATA_FD_COUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 *loghead = 1;
Christoph Hellwig62479f52019-11-08 15:05:34 -0800659
Christoph Hellwigd73e1ce2019-11-08 15:05:38 -0800660 end = xfs_dir3_data_end_offset(geo, addr);
Christoph Hellwig62479f52019-11-08 15:05:34 -0800661 while (offset < end) {
662 struct xfs_dir2_data_unused *dup = addr + offset;
663 struct xfs_dir2_data_entry *dep = addr + offset;
664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 /*
666 * If it's a free entry, insert it.
667 */
Nathan Scottad354eb2006-03-17 17:27:37 +1100668 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
Christoph Hellwig62479f52019-11-08 15:05:34 -0800669 ASSERT(offset ==
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000670 be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)));
Dave Chinner2ca98772013-10-29 22:11:49 +1100671 xfs_dir2_data_freeinsert(hdr, bf, dup, loghead);
Christoph Hellwig62479f52019-11-08 15:05:34 -0800672 offset += be16_to_cpu(dup->length);
673 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 }
Christoph Hellwig62479f52019-11-08 15:05:34 -0800675
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 /*
677 * For active entries, check their tags and skip them.
678 */
Christoph Hellwig7e8ae7bd2019-11-08 15:05:37 -0800679 ASSERT(offset ==
680 be16_to_cpu(*xfs_dir2_data_entry_tag_p(mp, dep)));
Christoph Hellwigfdbb8c52019-11-08 15:05:37 -0800681 offset += xfs_dir2_data_entsize(mp, dep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 }
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 */
689int /* error */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100690xfs_dir3_data_init(
Christoph Hellwigee641d52019-11-08 15:05:31 -0800691 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 Torvalds1da177e2005-04-16 15:20:36 -0700694{
Christoph Hellwigee641d52019-11-08 15:05:31 -0800695 struct xfs_trans *tp = args->trans;
696 struct xfs_inode *dp = args->dp;
697 struct xfs_mount *mp = dp->i_mount;
Christoph Hellwigd73e1ce2019-11-08 15:05:38 -0800698 struct xfs_da_geometry *geo = args->geo;
Christoph Hellwigee641d52019-11-08 15:05:31 -0800699 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 Torvalds1da177e2005-04-16 15:20:36 -0700705
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 /*
707 * Get the buffer set up for the block.
708 */
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000709 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(args->geo, blkno),
Christoph Hellwig2911edb2019-11-20 09:46:05 -0800710 &bp, XFS_DATA_FORK);
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100711 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 return error;
Dave Chinner33363fe2013-04-03 16:11:22 +1100713 bp->b_ops = &xfs_dir3_data_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100714 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_DATA_BUF);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200715
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 /*
717 * Initialize the header.
718 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000719 hdr = bp->b_addr;
Dave Chinner38c26bf2021-08-18 18:46:37 -0700720 if (xfs_has_crc(mp)) {
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100721 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 Sandeence748ea2015-07-29 11:53:31 +1000727 uuid_copy(&hdr3->uuid, &mp->m_sb.sb_meta_uuid);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100728
729 } else
730 hdr->magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
731
Christoph Hellwig1848b602019-11-08 15:05:39 -0800732 bf = xfs_dir2_data_bestfree_p(mp, hdr);
Christoph Hellwigd73e1ce2019-11-08 15:05:38 -0800733 bf[0].offset = cpu_to_be16(geo->data_entry_offset);
734 bf[0].length = cpu_to_be16(geo->blksize - geo->data_entry_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 for (i = 1; i < XFS_DIR2_DATA_FD_COUNT; i++) {
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100736 bf[i].length = 0;
737 bf[i].offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 }
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200739
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 /*
741 * Set up an unused entry for the block's body.
742 */
Christoph Hellwigd73e1ce2019-11-08 15:05:38 -0800743 dup = bp->b_addr + geo->data_entry_offset;
Nathan Scottad354eb2006-03-17 17:27:37 +1100744 dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
Christoph Hellwigee641d52019-11-08 15:05:31 -0800745 dup->length = bf[0].length;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200746 *xfs_dir2_data_unused_tag_p(dup) = cpu_to_be16((char *)dup - (char *)hdr);
Christoph Hellwigee641d52019-11-08 15:05:31 -0800747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 /*
749 * Log it and return it.
750 */
Dave Chinnerbc851782014-06-06 15:20:54 +1000751 xfs_dir2_data_log_header(args, bp);
752 xfs_dir2_data_log_unused(args, bp, dup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 *bpp = bp;
754 return 0;
755}
756
757/*
758 * Log an active data entry from the block.
759 */
760void
761xfs_dir2_data_log_entry(
Dave Chinnerbc851782014-06-06 15:20:54 +1000762 struct xfs_da_args *args,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000763 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 xfs_dir2_data_entry_t *dep) /* data entry pointer */
765{
Christoph Hellwig7e8ae7bd2019-11-08 15:05:37 -0800766 struct xfs_mount *mp = bp->b_mount;
Dave Chinner0cb97762013-08-12 20:50:09 +1000767 struct xfs_dir2_data_hdr *hdr = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200769 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
Dave Chinner33363fe2013-04-03 16:11:22 +1100770 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100771 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
772 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200773
Dave Chinnerbc851782014-06-06 15:20:54 +1000774 xfs_trans_log_buf(args->trans, bp, (uint)((char *)dep - (char *)hdr),
Christoph Hellwig7e8ae7bd2019-11-08 15:05:37 -0800775 (uint)((char *)(xfs_dir2_data_entry_tag_p(mp, dep) + 1) -
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200776 (char *)hdr - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777}
778
779/*
780 * Log a data block header.
781 */
782void
783xfs_dir2_data_log_header(
Dave Chinnerbc851782014-06-06 15:20:54 +1000784 struct xfs_da_args *args,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000785 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
Dave Chinner2ca98772013-10-29 22:11:49 +1100787#ifdef DEBUG
788 struct xfs_dir2_data_hdr *hdr = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200790 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
Dave Chinner33363fe2013-04-03 16:11:22 +1100791 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100792 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
793 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
Dave Chinner2ca98772013-10-29 22:11:49 +1100794#endif
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200795
Christoph Hellwigd73e1ce2019-11-08 15:05:38 -0800796 xfs_trans_log_buf(args->trans, bp, 0, args->geo->data_entry_offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797}
798
799/*
800 * Log a data unused entry.
801 */
802void
803xfs_dir2_data_log_unused(
Dave Chinnerbc851782014-06-06 15:20:54 +1000804 struct xfs_da_args *args,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000805 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 xfs_dir2_data_unused_t *dup) /* data unused pointer */
807{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000808 xfs_dir2_data_hdr_t *hdr = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200810 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
Dave Chinner33363fe2013-04-03 16:11:22 +1100811 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100812 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
813 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200814
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 /*
816 * Log the first part of the unused entry.
817 */
Dave Chinnerbc851782014-06-06 15:20:54 +1000818 xfs_trans_log_buf(args->trans, bp, (uint)((char *)dup - (char *)hdr),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 (uint)((char *)&dup->length + sizeof(dup->length) -
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200820 1 - (char *)hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 /*
822 * Log the end (tag) of the unused entry.
823 */
Dave Chinnerbc851782014-06-06 15:20:54 +1000824 xfs_trans_log_buf(args->trans, bp,
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200825 (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr),
826 (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 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 */
834void
835xfs_dir2_data_make_free(
Dave Chinnerbc851782014-06-06 15:20:54 +1000836 struct xfs_da_args *args,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000837 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 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 Hellwigc2066e22011-07-08 14:35:38 +0200843 xfs_dir2_data_hdr_t *hdr; /* data block pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 xfs_dir2_data_free_t *dfp; /* bestfree pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 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 Hellwig5c072122019-11-08 15:05:36 -0800849 unsigned int end;
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100850 struct xfs_dir2_data_free *bf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Dave Chinner1d9025e2012-06-22 18:50:14 +1000852 hdr = bp->b_addr;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200853
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 /*
855 * Figure out where the end of the data area is.
856 */
Christoph Hellwig5c072122019-11-08 15:05:36 -0800857 end = xfs_dir3_data_end_offset(args->geo, hdr);
858 ASSERT(end != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 /*
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 Hellwigd73e1ce2019-11-08 15:05:38 -0800864 if (offset > args->geo->data_entry_offset) {
Nathan Scott3d693c62006-03-17 17:28:27 +1100865 __be16 *tagp; /* tag just before us */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200867 tagp = (__be16 *)((char *)hdr + offset) - 1;
868 prevdup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
Nathan Scottad354eb2006-03-17 17:27:37 +1100869 if (be16_to_cpu(prevdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 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 Hellwig5c072122019-11-08 15:05:36 -0800877 if (offset + len < end) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 postdup =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200879 (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
Nathan Scottad354eb2006-03-17 17:27:37 +1100880 if (be16_to_cpu(postdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 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 Hellwig1848b602019-11-08 15:05:39 -0800890 bf = xfs_dir2_data_bestfree_p(args->dp->i_mount, hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 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 Chinner2ca98772013-10-29 22:11:49 +1100897 dfp = xfs_dir2_data_freefind(hdr, bf, prevdup);
898 dfp2 = xfs_dir2_data_freefind(hdr, bf, postdup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 /*
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 Chinnerf5f3d9b2013-04-03 16:11:20 +1100905 needscan = (bf[2].length != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 /*
907 * Fix up the new big freespace.
908 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800909 be16_add_cpu(&prevdup->length, len + be16_to_cpu(postdup->length));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000910 *xfs_dir2_data_unused_tag_p(prevdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200911 cpu_to_be16((char *)prevdup - (char *)hdr);
Dave Chinnerbc851782014-06-06 15:20:54 +1000912 xfs_dir2_data_log_unused(args, bp, prevdup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 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 Chinnerf5f3d9b2013-04-03 16:11:20 +1100921 if (dfp == &bf[1]) {
922 dfp = &bf[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 ASSERT(dfp2 == dfp);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100924 dfp2 = &bf[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 }
Dave Chinner2ca98772013-10-29 22:11:49 +1100926 xfs_dir2_data_freeremove(hdr, bf, dfp2, needlogp);
927 xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 /*
929 * Now insert the new entry.
930 */
Dave Chinner2ca98772013-10-29 22:11:49 +1100931 dfp = xfs_dir2_data_freeinsert(hdr, bf, prevdup,
932 needlogp);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100933 ASSERT(dfp == &bf[0]);
Nathan Scottad354eb2006-03-17 17:27:37 +1100934 ASSERT(dfp->length == prevdup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 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 Chinner2ca98772013-10-29 22:11:49 +1100943 dfp = xfs_dir2_data_freefind(hdr, bf, prevdup);
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800944 be16_add_cpu(&prevdup->length, len);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000945 *xfs_dir2_data_unused_tag_p(prevdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200946 cpu_to_be16((char *)prevdup - (char *)hdr);
Dave Chinnerbc851782014-06-06 15:20:54 +1000947 xfs_dir2_data_log_unused(args, bp, prevdup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 /*
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 Chinner2ca98772013-10-29 22:11:49 +1100954 xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp);
955 xfs_dir2_data_freeinsert(hdr, bf, prevdup, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 }
957 /*
958 * Otherwise we need a scan if the new entry is big enough.
959 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100960 else {
Nathan Scottad354eb2006-03-17 17:27:37 +1100961 needscan = be16_to_cpu(prevdup->length) >
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100962 be16_to_cpu(bf[2].length);
Nathan Scott70e73f52006-03-17 17:26:52 +1100963 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 }
965 /*
966 * The following entry is free, merge with it.
967 */
968 else if (postdup) {
Dave Chinner2ca98772013-10-29 22:11:49 +1100969 dfp = xfs_dir2_data_freefind(hdr, bf, postdup);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200970 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
Nathan Scottad354eb2006-03-17 17:27:37 +1100971 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
972 newdup->length = cpu_to_be16(len + be16_to_cpu(postdup->length));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000973 *xfs_dir2_data_unused_tag_p(newdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200974 cpu_to_be16((char *)newdup - (char *)hdr);
Dave Chinnerbc851782014-06-06 15:20:54 +1000975 xfs_dir2_data_log_unused(args, bp, newdup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 /*
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 Chinner2ca98772013-10-29 22:11:49 +1100982 xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp);
983 xfs_dir2_data_freeinsert(hdr, bf, newdup, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 }
985 /*
986 * Otherwise we need a scan if the new entry is big enough.
987 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100988 else {
Nathan Scottad354eb2006-03-17 17:27:37 +1100989 needscan = be16_to_cpu(newdup->length) >
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100990 be16_to_cpu(bf[2].length);
Nathan Scott70e73f52006-03-17 17:26:52 +1100991 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 }
993 /*
994 * Neither neighbor is free. Make a new entry.
995 */
996 else {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200997 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
Nathan Scottad354eb2006-03-17 17:27:37 +1100998 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
999 newdup->length = cpu_to_be16(len);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001000 *xfs_dir2_data_unused_tag_p(newdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001001 cpu_to_be16((char *)newdup - (char *)hdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001002 xfs_dir2_data_log_unused(args, bp, newdup);
Dave Chinner2ca98772013-10-29 22:11:49 +11001003 xfs_dir2_data_freeinsert(hdr, bf, newdup, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 }
1005 *needscanp = needscan;
1006}
1007
Darrick J. Wong6915ef32018-03-23 10:06:51 -07001008/* Check our free data for obvious signs of corruption. */
1009static inline xfs_failaddr_t
1010xfs_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. */
1034static inline xfs_failaddr_t
1035xfs_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 Torvalds1da177e2005-04-16 15:20:36 -07001049/*
1050 * Take a byte range out of an existing unused space and make it un-free.
1051 */
Darrick J. Wong6915ef32018-03-23 10:06:51 -07001052int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053xfs_dir2_data_use_free(
Dave Chinnerbc851782014-06-06 15:20:54 +10001054 struct xfs_da_args *args,
Dave Chinner1d9025e2012-06-22 18:50:14 +10001055 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 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 Hellwigc2066e22011-07-08 14:35:38 +02001062 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 xfs_dir2_data_free_t *dfp; /* bestfree pointer */
Darrick J. Wong6915ef32018-03-23 10:06:51 -07001064 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 Torvalds1da177e2005-04-16 15:20:36 -07001068 int matchback; /* matches end of freespace */
1069 int matchfront; /* matches start of freespace */
1070 int needscan; /* need to regen bestfree */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 int oldlen; /* old unused entry's length */
1072
Dave Chinner1d9025e2012-06-22 18:50:14 +10001073 hdr = bp->b_addr;
Darrick J. Wong6915ef32018-03-23 10:06:51 -07001074 fa = xfs_dir2_data_check_free(hdr, dup, offset, len);
1075 if (fa)
1076 goto corrupt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 /*
1078 * Look up the entry in the bestfree table.
1079 */
Nathan Scottad354eb2006-03-17 17:27:37 +11001080 oldlen = be16_to_cpu(dup->length);
Christoph Hellwig1848b602019-11-08 15:05:39 -08001081 bf = xfs_dir2_data_bestfree_p(args->dp->i_mount, hdr);
Dave Chinner2ca98772013-10-29 22:11:49 +11001082 dfp = xfs_dir2_data_freefind(hdr, bf, dup);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +11001083 ASSERT(dfp || oldlen <= be16_to_cpu(bf[2].length));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 /*
1085 * Check for alignment with front and back of the entry.
1086 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001087 matchfront = (char *)dup - (char *)hdr == offset;
1088 matchback = (char *)dup + oldlen - (char *)hdr == offset + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 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 Chinnerf5f3d9b2013-04-03 16:11:20 +11001097 needscan = (bf[2].offset != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 if (!needscan)
Dave Chinner2ca98772013-10-29 22:11:49 +11001099 xfs_dir2_data_freeremove(hdr, bf, dfp,
1100 needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 }
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 Hellwigc2066e22011-07-08 14:35:38 +02001108 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
Nathan Scottad354eb2006-03-17 17:27:37 +11001109 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
1110 newdup->length = cpu_to_be16(oldlen - len);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001111 *xfs_dir2_data_unused_tag_p(newdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001112 cpu_to_be16((char *)newdup - (char *)hdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001113 xfs_dir2_data_log_unused(args, bp, newdup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 /*
1115 * If it was in the table, remove it and add the new one.
1116 */
1117 if (dfp) {
Dave Chinner2ca98772013-10-29 22:11:49 +11001118 xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp);
1119 dfp = xfs_dir2_data_freeinsert(hdr, bf, newdup,
1120 needlogp);
Darrick J. Wong6915ef32018-03-23 10:06:51 -07001121 fa = xfs_dir2_data_check_new_free(hdr, dfp, newdup);
1122 if (fa)
1123 goto corrupt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 /*
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 Chinnerf5f3d9b2013-04-03 16:11:20 +11001129 needscan = dfp == &bf[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 }
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 Hellwigc2066e22011-07-08 14:35:38 +02001138 newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001139 *xfs_dir2_data_unused_tag_p(newdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001140 cpu_to_be16((char *)newdup - (char *)hdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001141 xfs_dir2_data_log_unused(args, bp, newdup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 /*
1143 * If it was in the table, remove it and add the new one.
1144 */
1145 if (dfp) {
Dave Chinner2ca98772013-10-29 22:11:49 +11001146 xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp);
1147 dfp = xfs_dir2_data_freeinsert(hdr, bf, newdup,
1148 needlogp);
Darrick J. Wong6915ef32018-03-23 10:06:51 -07001149 fa = xfs_dir2_data_check_new_free(hdr, dfp, newdup);
1150 if (fa)
1151 goto corrupt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 /*
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 Chinnerf5f3d9b2013-04-03 16:11:20 +11001157 needscan = dfp == &bf[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 }
1159 }
1160 /*
1161 * Poking out the middle of an entry.
1162 * Make two new entries.
1163 */
1164 else {
1165 newdup = dup;
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001166 newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001167 *xfs_dir2_data_unused_tag_p(newdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001168 cpu_to_be16((char *)newdup - (char *)hdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001169 xfs_dir2_data_log_unused(args, bp, newdup);
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001170 newdup2 = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
Nathan Scottad354eb2006-03-17 17:27:37 +11001171 newdup2->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
1172 newdup2->length = cpu_to_be16(oldlen - len - be16_to_cpu(newdup->length));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001173 *xfs_dir2_data_unused_tag_p(newdup2) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001174 cpu_to_be16((char *)newdup2 - (char *)hdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001175 xfs_dir2_data_log_unused(args, bp, newdup2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 /*
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 Chinnerf5f3d9b2013-04-03 16:11:20 +11001185 needscan = (bf[2].length != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 if (!needscan) {
Dave Chinner2ca98772013-10-29 22:11:49 +11001187 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 Hellwigc2066e22011-07-08 14:35:38 +02001192 needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 }
1194 }
1195 }
1196 *needscanp = needscan;
Darrick J. Wong6915ef32018-03-23 10:06:51 -07001197 return 0;
1198corrupt:
1199 xfs_corruption_error(__func__, XFS_ERRLEVEL_LOW, args->dp->i_mount,
Darrick J. Wong2551a532018-06-04 10:23:54 -07001200 hdr, sizeof(*hdr), __FILE__, __LINE__, fa);
Darrick J. Wong6915ef32018-03-23 10:06:51 -07001201 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202}
Darrick J. Wongce92d292018-01-16 18:54:12 -08001203
1204/* Find the end of the entry data in a data/block format dir block. */
Christoph Hellwig5c072122019-11-08 15:05:36 -08001205unsigned int
1206xfs_dir3_data_end_offset(
Darrick J. Wongce92d292018-01-16 18:54:12 -08001207 struct xfs_da_geometry *geo,
1208 struct xfs_dir2_data_hdr *hdr)
1209{
Christoph Hellwig5c072122019-11-08 15:05:36 -08001210 void *p;
1211
Darrick J. Wongce92d292018-01-16 18:54:12 -08001212 switch (hdr->magic) {
1213 case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC):
1214 case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC):
Christoph Hellwig5c072122019-11-08 15:05:36 -08001215 p = xfs_dir2_block_leaf_p(xfs_dir2_block_tail_p(geo, hdr));
1216 return p - (void *)hdr;
Darrick J. Wongce92d292018-01-16 18:54:12 -08001217 case cpu_to_be32(XFS_DIR3_DATA_MAGIC):
1218 case cpu_to_be32(XFS_DIR2_DATA_MAGIC):
Christoph Hellwig5c072122019-11-08 15:05:36 -08001219 return geo->blksize;
Darrick J. Wongce92d292018-01-16 18:54:12 -08001220 default:
Christoph Hellwig5c072122019-11-08 15:05:36 -08001221 return 0;
Darrick J. Wongce92d292018-01-16 18:54:12 -08001222 }
1223}