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