2018-06-06 02:42:14 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
2005-11-02 03:58:39 +00:00
|
|
|
* Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
|
2013-04-11 21:30:21 +00:00
|
|
|
* Copyright (c) 2013 Red Hat, Inc.
|
2005-11-02 03:58:39 +00:00
|
|
|
* All Rights Reserved.
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
|
|
|
#include "xfs.h"
|
2005-11-02 03:38:42 +00:00
|
|
|
#include "xfs_fs.h"
|
2019-06-29 02:25:35 +00:00
|
|
|
#include "xfs_shared.h"
|
2013-10-22 23:51:50 +00:00
|
|
|
#include "xfs_format.h"
|
2013-10-22 23:50:10 +00:00
|
|
|
#include "xfs_log_format.h"
|
|
|
|
#include "xfs_trans_resv.h"
|
2005-04-16 22:20:36 +00:00
|
|
|
#include "xfs_mount.h"
|
|
|
|
#include "xfs_inode.h"
|
|
|
|
#include "xfs_bmap.h"
|
2013-08-12 10:49:37 +00:00
|
|
|
#include "xfs_dir2.h"
|
2011-07-13 11:43:48 +00:00
|
|
|
#include "xfs_dir2_priv.h"
|
2005-04-16 22:20:36 +00:00
|
|
|
#include "xfs_error.h"
|
2009-12-14 23:14:59 +00:00
|
|
|
#include "xfs_trace.h"
|
2013-10-22 23:50:10 +00:00
|
|
|
#include "xfs_trans.h"
|
2013-04-11 21:30:21 +00:00
|
|
|
#include "xfs_buf_item.h"
|
2024-02-22 20:32:18 +00:00
|
|
|
#include "xfs_health.h"
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Local function declarations.
|
|
|
|
*/
|
2012-06-22 08:50:14 +00:00
|
|
|
static int xfs_dir2_leaf_lookup_int(xfs_da_args_t *args, struct xfs_buf **lbpp,
|
2019-11-08 22:57:50 +00:00
|
|
|
int *indexp, struct xfs_buf **dbpp,
|
|
|
|
struct xfs_dir3_icleaf_hdr *leafhdr);
|
2014-06-06 05:20:54 +00:00
|
|
|
static void xfs_dir3_leaf_log_bests(struct xfs_da_args *args,
|
|
|
|
struct xfs_buf *bp, int first, int last);
|
|
|
|
static void xfs_dir3_leaf_log_tail(struct xfs_da_args *args,
|
|
|
|
struct xfs_buf *bp);
|
2005-06-21 05:36:52 +00:00
|
|
|
|
2019-11-08 22:57:49 +00:00
|
|
|
void
|
|
|
|
xfs_dir2_leaf_hdr_from_disk(
|
|
|
|
struct xfs_mount *mp,
|
|
|
|
struct xfs_dir3_icleaf_hdr *to,
|
|
|
|
struct xfs_dir2_leaf *from)
|
|
|
|
{
|
2021-08-19 01:46:55 +00:00
|
|
|
if (xfs_has_crc(mp)) {
|
2019-11-08 22:57:49 +00:00
|
|
|
struct xfs_dir3_leaf *from3 = (struct xfs_dir3_leaf *)from;
|
|
|
|
|
|
|
|
to->forw = be32_to_cpu(from3->hdr.info.hdr.forw);
|
|
|
|
to->back = be32_to_cpu(from3->hdr.info.hdr.back);
|
|
|
|
to->magic = be16_to_cpu(from3->hdr.info.hdr.magic);
|
|
|
|
to->count = be16_to_cpu(from3->hdr.count);
|
|
|
|
to->stale = be16_to_cpu(from3->hdr.stale);
|
2019-11-08 22:57:50 +00:00
|
|
|
to->ents = from3->__ents;
|
2019-11-08 22:57:49 +00:00
|
|
|
|
|
|
|
ASSERT(to->magic == XFS_DIR3_LEAF1_MAGIC ||
|
|
|
|
to->magic == XFS_DIR3_LEAFN_MAGIC);
|
|
|
|
} else {
|
|
|
|
to->forw = be32_to_cpu(from->hdr.info.forw);
|
|
|
|
to->back = be32_to_cpu(from->hdr.info.back);
|
|
|
|
to->magic = be16_to_cpu(from->hdr.info.magic);
|
|
|
|
to->count = be16_to_cpu(from->hdr.count);
|
|
|
|
to->stale = be16_to_cpu(from->hdr.stale);
|
2019-11-08 22:57:50 +00:00
|
|
|
to->ents = from->__ents;
|
2019-11-08 22:57:49 +00:00
|
|
|
|
|
|
|
ASSERT(to->magic == XFS_DIR2_LEAF1_MAGIC ||
|
|
|
|
to->magic == XFS_DIR2_LEAFN_MAGIC);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-08 22:57:50 +00:00
|
|
|
void
|
|
|
|
xfs_dir2_leaf_hdr_to_disk(
|
|
|
|
struct xfs_mount *mp,
|
|
|
|
struct xfs_dir2_leaf *to,
|
|
|
|
struct xfs_dir3_icleaf_hdr *from)
|
|
|
|
{
|
2021-08-19 01:46:55 +00:00
|
|
|
if (xfs_has_crc(mp)) {
|
2019-11-08 22:57:50 +00:00
|
|
|
struct xfs_dir3_leaf *to3 = (struct xfs_dir3_leaf *)to;
|
|
|
|
|
|
|
|
ASSERT(from->magic == XFS_DIR3_LEAF1_MAGIC ||
|
|
|
|
from->magic == XFS_DIR3_LEAFN_MAGIC);
|
|
|
|
|
|
|
|
to3->hdr.info.hdr.forw = cpu_to_be32(from->forw);
|
|
|
|
to3->hdr.info.hdr.back = cpu_to_be32(from->back);
|
|
|
|
to3->hdr.info.hdr.magic = cpu_to_be16(from->magic);
|
|
|
|
to3->hdr.count = cpu_to_be16(from->count);
|
|
|
|
to3->hdr.stale = cpu_to_be16(from->stale);
|
|
|
|
} else {
|
|
|
|
ASSERT(from->magic == XFS_DIR2_LEAF1_MAGIC ||
|
|
|
|
from->magic == XFS_DIR2_LEAFN_MAGIC);
|
|
|
|
|
|
|
|
to->hdr.info.forw = cpu_to_be32(from->forw);
|
|
|
|
to->hdr.info.back = cpu_to_be32(from->back);
|
|
|
|
to->hdr.info.magic = cpu_to_be16(from->magic);
|
|
|
|
to->hdr.count = cpu_to_be16(from->count);
|
|
|
|
to->hdr.stale = cpu_to_be16(from->stale);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-11 21:30:21 +00:00
|
|
|
/*
|
|
|
|
* Check the internal consistency of a leaf1 block.
|
|
|
|
* Pop an assert if something is wrong.
|
|
|
|
*/
|
|
|
|
#ifdef DEBUG
|
2018-01-08 18:51:03 +00:00
|
|
|
static xfs_failaddr_t
|
2013-04-11 21:30:21 +00:00
|
|
|
xfs_dir3_leaf1_check(
|
2013-10-29 11:11:50 +00:00
|
|
|
struct xfs_inode *dp,
|
2013-04-11 21:30:21 +00:00
|
|
|
struct xfs_buf *bp)
|
|
|
|
{
|
|
|
|
struct xfs_dir2_leaf *leaf = bp->b_addr;
|
|
|
|
struct xfs_dir3_icleaf_hdr leafhdr;
|
|
|
|
|
2019-11-08 22:57:49 +00:00
|
|
|
xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
|
2013-04-11 21:30:21 +00:00
|
|
|
|
|
|
|
if (leafhdr.magic == XFS_DIR3_LEAF1_MAGIC) {
|
|
|
|
struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
|
2021-08-19 01:47:05 +00:00
|
|
|
if (be64_to_cpu(leaf3->info.blkno) != xfs_buf_daddr(bp))
|
2018-01-08 18:51:03 +00:00
|
|
|
return __this_address;
|
2013-04-11 21:30:21 +00:00
|
|
|
} else if (leafhdr.magic != XFS_DIR2_LEAF1_MAGIC)
|
2018-01-08 18:51:03 +00:00
|
|
|
return __this_address;
|
2013-04-11 21:30:21 +00:00
|
|
|
|
2021-03-22 16:52:05 +00:00
|
|
|
return xfs_dir3_leaf_check_int(dp->i_mount, &leafhdr, leaf, false);
|
2013-04-11 21:30:21 +00:00
|
|
|
}
|
2018-01-08 18:51:03 +00:00
|
|
|
|
|
|
|
static inline void
|
|
|
|
xfs_dir3_leaf_check(
|
|
|
|
struct xfs_inode *dp,
|
|
|
|
struct xfs_buf *bp)
|
|
|
|
{
|
|
|
|
xfs_failaddr_t fa;
|
|
|
|
|
|
|
|
fa = xfs_dir3_leaf1_check(dp, bp);
|
|
|
|
if (!fa)
|
|
|
|
return;
|
|
|
|
xfs_corruption_error(__func__, XFS_ERRLEVEL_LOW, dp->i_mount,
|
2018-06-04 17:23:54 +00:00
|
|
|
bp->b_addr, BBTOB(bp->b_length), __FILE__, __LINE__,
|
|
|
|
fa);
|
2018-01-08 18:51:03 +00:00
|
|
|
ASSERT(0);
|
|
|
|
}
|
2013-04-11 21:30:21 +00:00
|
|
|
#else
|
2013-10-29 11:11:50 +00:00
|
|
|
#define xfs_dir3_leaf_check(dp, bp)
|
2013-04-11 21:30:21 +00:00
|
|
|
#endif
|
|
|
|
|
2018-01-08 18:51:03 +00:00
|
|
|
xfs_failaddr_t
|
2013-04-11 21:30:21 +00:00
|
|
|
xfs_dir3_leaf_check_int(
|
2019-11-08 23:06:03 +00:00
|
|
|
struct xfs_mount *mp,
|
|
|
|
struct xfs_dir3_icleaf_hdr *hdr,
|
2021-03-22 16:52:05 +00:00
|
|
|
struct xfs_dir2_leaf *leaf,
|
|
|
|
bool expensive_checking)
|
2013-04-11 21:30:21 +00:00
|
|
|
{
|
2019-11-08 23:06:03 +00:00
|
|
|
struct xfs_da_geometry *geo = mp->m_dir_geo;
|
|
|
|
xfs_dir2_leaf_tail_t *ltp;
|
|
|
|
int stale;
|
|
|
|
int i;
|
xfs: fix exception caused by unexpected illegal bestcount in leaf dir
For leaf dir, In most cases, there should be as many bestfree slots
as the dir data blocks that can fit under i_size (except for [1]).
Root cause is we don't examin the number bestfree slots, when the slots
number less than dir data blocks, if we need to allocate new dir data
block and update the bestfree array, we will use the dir block number as
index to assign bestfree array, while we did not check the leaf buf
boundary which may cause UAF or other memory access problem. This issue
can also triggered with test cases xfs/473 from fstests.
According to Dave Chinner & Darrick's suggestion, adding buffer verifier
to detect this abnormal situation in time.
Simplify the testcase for fstest xfs/554 [1]
The error log is shown as follows:
==================================================================
BUG: KASAN: use-after-free in xfs_dir2_leaf_addname+0x1995/0x1ac0
Write of size 2 at addr ffff88810168b000 by task touch/1552
CPU: 5 PID: 1552 Comm: touch Not tainted 6.0.0-rc3+ #101
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS
1.13.0-1ubuntu1.1 04/01/2014
Call Trace:
<TASK>
dump_stack_lvl+0x4d/0x66
print_report.cold+0xf6/0x691
kasan_report+0xa8/0x120
xfs_dir2_leaf_addname+0x1995/0x1ac0
xfs_dir_createname+0x58c/0x7f0
xfs_create+0x7af/0x1010
xfs_generic_create+0x270/0x5e0
path_openat+0x270b/0x3450
do_filp_open+0x1cf/0x2b0
do_sys_openat2+0x46b/0x7a0
do_sys_open+0xb7/0x130
do_syscall_64+0x35/0x80
entry_SYSCALL_64_after_hwframe+0x63/0xcd
RIP: 0033:0x7fe4d9e9312b
Code: 25 00 00 41 00 3d 00 00 41 00 74 4b 64 8b 04 25 18 00 00 00 85 c0
75 67 44 89 e2 48 89 ee bf 9c ff ff ff b8 01 01 00 00 0f 05 <48> 3d 00
f0 ff ff 0f 87 91 00 00 00 48 8b 4c 24 28 64 48 33 0c 25
RSP: 002b:00007ffda4c16c20 EFLAGS: 00000246 ORIG_RAX: 0000000000000101
RAX: ffffffffffffffda RBX: 0000000000000001 RCX: 00007fe4d9e9312b
RDX: 0000000000000941 RSI: 00007ffda4c17f33 RDI: 00000000ffffff9c
RBP: 00007ffda4c17f33 R08: 0000000000000000 R09: 0000000000000000
R10: 00000000000001b6 R11: 0000000000000246 R12: 0000000000000941
R13: 00007fe4d9f631a4 R14: 00007ffda4c17f33 R15: 0000000000000000
</TASK>
The buggy address belongs to the physical page:
page:ffffea000405a2c0 refcount:0 mapcount:0 mapping:0000000000000000
index:0x0 pfn:0x10168b
flags: 0x2fffff80000000(node=0|zone=2|lastcpupid=0x1fffff)
raw: 002fffff80000000 ffffea0004057788 ffffea000402dbc8 0000000000000000
raw: 0000000000000000 0000000000170000 00000000ffffffff 0000000000000000
page dumped because: kasan: bad access detected
Memory state around the buggy address:
ffff88810168af00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
ffff88810168af80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
>ffff88810168b000: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
^
ffff88810168b080: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
ffff88810168b100: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
==================================================================
Disabling lock debugging due to kernel taint
00000000: 58 44 44 33 5b 53 35 c2 00 00 00 00 00 00 00 78
XDD3[S5........x
XFS (sdb): Internal error xfs_dir2_data_use_free at line 1200 of file
fs/xfs/libxfs/xfs_dir2_data.c. Caller
xfs_dir2_data_use_free+0x28a/0xeb0
CPU: 5 PID: 1552 Comm: touch Tainted: G B 6.0.0-rc3+
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS
1.13.0-1ubuntu1.1 04/01/2014
Call Trace:
<TASK>
dump_stack_lvl+0x4d/0x66
xfs_corruption_error+0x132/0x150
xfs_dir2_data_use_free+0x198/0xeb0
xfs_dir2_leaf_addname+0xa59/0x1ac0
xfs_dir_createname+0x58c/0x7f0
xfs_create+0x7af/0x1010
xfs_generic_create+0x270/0x5e0
path_openat+0x270b/0x3450
do_filp_open+0x1cf/0x2b0
do_sys_openat2+0x46b/0x7a0
do_sys_open+0xb7/0x130
do_syscall_64+0x35/0x80
entry_SYSCALL_64_after_hwframe+0x63/0xcd
RIP: 0033:0x7fe4d9e9312b
Code: 25 00 00 41 00 3d 00 00 41 00 74 4b 64 8b 04 25 18 00 00 00 85 c0
75 67 44 89 e2 48 89 ee bf 9c ff ff ff b8 01 01 00 00 0f 05 <48> 3d 00
f0 ff ff 0f 87 91 00 00 00 48 8b 4c 24 28 64 48 33 0c 25
RSP: 002b:00007ffda4c16c20 EFLAGS: 00000246 ORIG_RAX: 0000000000000101
RAX: ffffffffffffffda RBX: 0000000000000001 RCX: 00007fe4d9e9312b
RDX: 0000000000000941 RSI: 00007ffda4c17f46 RDI: 00000000ffffff9c
RBP: 00007ffda4c17f46 R08: 0000000000000000 R09: 0000000000000001
R10: 00000000000001b6 R11: 0000000000000246 R12: 0000000000000941
R13: 00007fe4d9f631a4 R14: 00007ffda4c17f46 R15: 0000000000000000
</TASK>
XFS (sdb): Corruption detected. Unmount and run xfs_repair
[1] https://lore.kernel.org/all/20220928095355.2074025-1-guoxuenan@huawei.com/
Reviewed-by: Hou Tao <houtao1@huawei.com>
Signed-off-by: Guo Xuenan <guoxuenan@huawei.com>
Reviewed-by: Darrick J. Wong <djwong@kernel.org>
Signed-off-by: Darrick J. Wong <djwong@kernel.org>
2022-10-18 21:32:35 +00:00
|
|
|
bool isleaf1 = (hdr->magic == XFS_DIR2_LEAF1_MAGIC ||
|
|
|
|
hdr->magic == XFS_DIR3_LEAF1_MAGIC);
|
2013-10-29 11:11:52 +00:00
|
|
|
|
2014-06-06 05:15:59 +00:00
|
|
|
ltp = xfs_dir2_leaf_tail_p(geo, leaf);
|
2013-04-11 21:30:21 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* XXX (dgc): This value is not restrictive enough.
|
|
|
|
* Should factor in the size of the bests table as well.
|
2021-03-29 18:11:40 +00:00
|
|
|
* We can deduce a value for that from i_disk_size.
|
2013-04-11 21:30:21 +00:00
|
|
|
*/
|
2019-11-08 22:57:51 +00:00
|
|
|
if (hdr->count > geo->leaf_max_ents)
|
2018-01-08 18:51:03 +00:00
|
|
|
return __this_address;
|
2013-04-11 21:30:21 +00:00
|
|
|
|
|
|
|
/* Leaves and bests don't overlap in leaf format. */
|
xfs: fix exception caused by unexpected illegal bestcount in leaf dir
For leaf dir, In most cases, there should be as many bestfree slots
as the dir data blocks that can fit under i_size (except for [1]).
Root cause is we don't examin the number bestfree slots, when the slots
number less than dir data blocks, if we need to allocate new dir data
block and update the bestfree array, we will use the dir block number as
index to assign bestfree array, while we did not check the leaf buf
boundary which may cause UAF or other memory access problem. This issue
can also triggered with test cases xfs/473 from fstests.
According to Dave Chinner & Darrick's suggestion, adding buffer verifier
to detect this abnormal situation in time.
Simplify the testcase for fstest xfs/554 [1]
The error log is shown as follows:
==================================================================
BUG: KASAN: use-after-free in xfs_dir2_leaf_addname+0x1995/0x1ac0
Write of size 2 at addr ffff88810168b000 by task touch/1552
CPU: 5 PID: 1552 Comm: touch Not tainted 6.0.0-rc3+ #101
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS
1.13.0-1ubuntu1.1 04/01/2014
Call Trace:
<TASK>
dump_stack_lvl+0x4d/0x66
print_report.cold+0xf6/0x691
kasan_report+0xa8/0x120
xfs_dir2_leaf_addname+0x1995/0x1ac0
xfs_dir_createname+0x58c/0x7f0
xfs_create+0x7af/0x1010
xfs_generic_create+0x270/0x5e0
path_openat+0x270b/0x3450
do_filp_open+0x1cf/0x2b0
do_sys_openat2+0x46b/0x7a0
do_sys_open+0xb7/0x130
do_syscall_64+0x35/0x80
entry_SYSCALL_64_after_hwframe+0x63/0xcd
RIP: 0033:0x7fe4d9e9312b
Code: 25 00 00 41 00 3d 00 00 41 00 74 4b 64 8b 04 25 18 00 00 00 85 c0
75 67 44 89 e2 48 89 ee bf 9c ff ff ff b8 01 01 00 00 0f 05 <48> 3d 00
f0 ff ff 0f 87 91 00 00 00 48 8b 4c 24 28 64 48 33 0c 25
RSP: 002b:00007ffda4c16c20 EFLAGS: 00000246 ORIG_RAX: 0000000000000101
RAX: ffffffffffffffda RBX: 0000000000000001 RCX: 00007fe4d9e9312b
RDX: 0000000000000941 RSI: 00007ffda4c17f33 RDI: 00000000ffffff9c
RBP: 00007ffda4c17f33 R08: 0000000000000000 R09: 0000000000000000
R10: 00000000000001b6 R11: 0000000000000246 R12: 0000000000000941
R13: 00007fe4d9f631a4 R14: 00007ffda4c17f33 R15: 0000000000000000
</TASK>
The buggy address belongs to the physical page:
page:ffffea000405a2c0 refcount:0 mapcount:0 mapping:0000000000000000
index:0x0 pfn:0x10168b
flags: 0x2fffff80000000(node=0|zone=2|lastcpupid=0x1fffff)
raw: 002fffff80000000 ffffea0004057788 ffffea000402dbc8 0000000000000000
raw: 0000000000000000 0000000000170000 00000000ffffffff 0000000000000000
page dumped because: kasan: bad access detected
Memory state around the buggy address:
ffff88810168af00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
ffff88810168af80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
>ffff88810168b000: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
^
ffff88810168b080: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
ffff88810168b100: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
==================================================================
Disabling lock debugging due to kernel taint
00000000: 58 44 44 33 5b 53 35 c2 00 00 00 00 00 00 00 78
XDD3[S5........x
XFS (sdb): Internal error xfs_dir2_data_use_free at line 1200 of file
fs/xfs/libxfs/xfs_dir2_data.c. Caller
xfs_dir2_data_use_free+0x28a/0xeb0
CPU: 5 PID: 1552 Comm: touch Tainted: G B 6.0.0-rc3+
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS
1.13.0-1ubuntu1.1 04/01/2014
Call Trace:
<TASK>
dump_stack_lvl+0x4d/0x66
xfs_corruption_error+0x132/0x150
xfs_dir2_data_use_free+0x198/0xeb0
xfs_dir2_leaf_addname+0xa59/0x1ac0
xfs_dir_createname+0x58c/0x7f0
xfs_create+0x7af/0x1010
xfs_generic_create+0x270/0x5e0
path_openat+0x270b/0x3450
do_filp_open+0x1cf/0x2b0
do_sys_openat2+0x46b/0x7a0
do_sys_open+0xb7/0x130
do_syscall_64+0x35/0x80
entry_SYSCALL_64_after_hwframe+0x63/0xcd
RIP: 0033:0x7fe4d9e9312b
Code: 25 00 00 41 00 3d 00 00 41 00 74 4b 64 8b 04 25 18 00 00 00 85 c0
75 67 44 89 e2 48 89 ee bf 9c ff ff ff b8 01 01 00 00 0f 05 <48> 3d 00
f0 ff ff 0f 87 91 00 00 00 48 8b 4c 24 28 64 48 33 0c 25
RSP: 002b:00007ffda4c16c20 EFLAGS: 00000246 ORIG_RAX: 0000000000000101
RAX: ffffffffffffffda RBX: 0000000000000001 RCX: 00007fe4d9e9312b
RDX: 0000000000000941 RSI: 00007ffda4c17f46 RDI: 00000000ffffff9c
RBP: 00007ffda4c17f46 R08: 0000000000000000 R09: 0000000000000001
R10: 00000000000001b6 R11: 0000000000000246 R12: 0000000000000941
R13: 00007fe4d9f631a4 R14: 00007ffda4c17f46 R15: 0000000000000000
</TASK>
XFS (sdb): Corruption detected. Unmount and run xfs_repair
[1] https://lore.kernel.org/all/20220928095355.2074025-1-guoxuenan@huawei.com/
Reviewed-by: Hou Tao <houtao1@huawei.com>
Signed-off-by: Guo Xuenan <guoxuenan@huawei.com>
Reviewed-by: Darrick J. Wong <djwong@kernel.org>
Signed-off-by: Darrick J. Wong <djwong@kernel.org>
2022-10-18 21:32:35 +00:00
|
|
|
if (isleaf1 &&
|
2019-11-08 22:57:50 +00:00
|
|
|
(char *)&hdr->ents[hdr->count] > (char *)xfs_dir2_leaf_bests_p(ltp))
|
2018-01-08 18:51:03 +00:00
|
|
|
return __this_address;
|
2013-04-11 21:30:21 +00:00
|
|
|
|
2021-03-22 16:52:05 +00:00
|
|
|
if (!expensive_checking)
|
|
|
|
return NULL;
|
|
|
|
|
2013-04-11 21:30:21 +00:00
|
|
|
/* Check hash value order, count stale entries. */
|
|
|
|
for (i = stale = 0; i < hdr->count; i++) {
|
|
|
|
if (i + 1 < hdr->count) {
|
2019-11-08 22:57:50 +00:00
|
|
|
if (be32_to_cpu(hdr->ents[i].hashval) >
|
|
|
|
be32_to_cpu(hdr->ents[i + 1].hashval))
|
2018-01-08 18:51:03 +00:00
|
|
|
return __this_address;
|
2013-04-11 21:30:21 +00:00
|
|
|
}
|
2019-11-08 22:57:50 +00:00
|
|
|
if (hdr->ents[i].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
|
2013-04-11 21:30:21 +00:00
|
|
|
stale++;
|
xfs: fix exception caused by unexpected illegal bestcount in leaf dir
For leaf dir, In most cases, there should be as many bestfree slots
as the dir data blocks that can fit under i_size (except for [1]).
Root cause is we don't examin the number bestfree slots, when the slots
number less than dir data blocks, if we need to allocate new dir data
block and update the bestfree array, we will use the dir block number as
index to assign bestfree array, while we did not check the leaf buf
boundary which may cause UAF or other memory access problem. This issue
can also triggered with test cases xfs/473 from fstests.
According to Dave Chinner & Darrick's suggestion, adding buffer verifier
to detect this abnormal situation in time.
Simplify the testcase for fstest xfs/554 [1]
The error log is shown as follows:
==================================================================
BUG: KASAN: use-after-free in xfs_dir2_leaf_addname+0x1995/0x1ac0
Write of size 2 at addr ffff88810168b000 by task touch/1552
CPU: 5 PID: 1552 Comm: touch Not tainted 6.0.0-rc3+ #101
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS
1.13.0-1ubuntu1.1 04/01/2014
Call Trace:
<TASK>
dump_stack_lvl+0x4d/0x66
print_report.cold+0xf6/0x691
kasan_report+0xa8/0x120
xfs_dir2_leaf_addname+0x1995/0x1ac0
xfs_dir_createname+0x58c/0x7f0
xfs_create+0x7af/0x1010
xfs_generic_create+0x270/0x5e0
path_openat+0x270b/0x3450
do_filp_open+0x1cf/0x2b0
do_sys_openat2+0x46b/0x7a0
do_sys_open+0xb7/0x130
do_syscall_64+0x35/0x80
entry_SYSCALL_64_after_hwframe+0x63/0xcd
RIP: 0033:0x7fe4d9e9312b
Code: 25 00 00 41 00 3d 00 00 41 00 74 4b 64 8b 04 25 18 00 00 00 85 c0
75 67 44 89 e2 48 89 ee bf 9c ff ff ff b8 01 01 00 00 0f 05 <48> 3d 00
f0 ff ff 0f 87 91 00 00 00 48 8b 4c 24 28 64 48 33 0c 25
RSP: 002b:00007ffda4c16c20 EFLAGS: 00000246 ORIG_RAX: 0000000000000101
RAX: ffffffffffffffda RBX: 0000000000000001 RCX: 00007fe4d9e9312b
RDX: 0000000000000941 RSI: 00007ffda4c17f33 RDI: 00000000ffffff9c
RBP: 00007ffda4c17f33 R08: 0000000000000000 R09: 0000000000000000
R10: 00000000000001b6 R11: 0000000000000246 R12: 0000000000000941
R13: 00007fe4d9f631a4 R14: 00007ffda4c17f33 R15: 0000000000000000
</TASK>
The buggy address belongs to the physical page:
page:ffffea000405a2c0 refcount:0 mapcount:0 mapping:0000000000000000
index:0x0 pfn:0x10168b
flags: 0x2fffff80000000(node=0|zone=2|lastcpupid=0x1fffff)
raw: 002fffff80000000 ffffea0004057788 ffffea000402dbc8 0000000000000000
raw: 0000000000000000 0000000000170000 00000000ffffffff 0000000000000000
page dumped because: kasan: bad access detected
Memory state around the buggy address:
ffff88810168af00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
ffff88810168af80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
>ffff88810168b000: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
^
ffff88810168b080: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
ffff88810168b100: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
==================================================================
Disabling lock debugging due to kernel taint
00000000: 58 44 44 33 5b 53 35 c2 00 00 00 00 00 00 00 78
XDD3[S5........x
XFS (sdb): Internal error xfs_dir2_data_use_free at line 1200 of file
fs/xfs/libxfs/xfs_dir2_data.c. Caller
xfs_dir2_data_use_free+0x28a/0xeb0
CPU: 5 PID: 1552 Comm: touch Tainted: G B 6.0.0-rc3+
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS
1.13.0-1ubuntu1.1 04/01/2014
Call Trace:
<TASK>
dump_stack_lvl+0x4d/0x66
xfs_corruption_error+0x132/0x150
xfs_dir2_data_use_free+0x198/0xeb0
xfs_dir2_leaf_addname+0xa59/0x1ac0
xfs_dir_createname+0x58c/0x7f0
xfs_create+0x7af/0x1010
xfs_generic_create+0x270/0x5e0
path_openat+0x270b/0x3450
do_filp_open+0x1cf/0x2b0
do_sys_openat2+0x46b/0x7a0
do_sys_open+0xb7/0x130
do_syscall_64+0x35/0x80
entry_SYSCALL_64_after_hwframe+0x63/0xcd
RIP: 0033:0x7fe4d9e9312b
Code: 25 00 00 41 00 3d 00 00 41 00 74 4b 64 8b 04 25 18 00 00 00 85 c0
75 67 44 89 e2 48 89 ee bf 9c ff ff ff b8 01 01 00 00 0f 05 <48> 3d 00
f0 ff ff 0f 87 91 00 00 00 48 8b 4c 24 28 64 48 33 0c 25
RSP: 002b:00007ffda4c16c20 EFLAGS: 00000246 ORIG_RAX: 0000000000000101
RAX: ffffffffffffffda RBX: 0000000000000001 RCX: 00007fe4d9e9312b
RDX: 0000000000000941 RSI: 00007ffda4c17f46 RDI: 00000000ffffff9c
RBP: 00007ffda4c17f46 R08: 0000000000000000 R09: 0000000000000001
R10: 00000000000001b6 R11: 0000000000000246 R12: 0000000000000941
R13: 00007fe4d9f631a4 R14: 00007ffda4c17f46 R15: 0000000000000000
</TASK>
XFS (sdb): Corruption detected. Unmount and run xfs_repair
[1] https://lore.kernel.org/all/20220928095355.2074025-1-guoxuenan@huawei.com/
Reviewed-by: Hou Tao <houtao1@huawei.com>
Signed-off-by: Guo Xuenan <guoxuenan@huawei.com>
Reviewed-by: Darrick J. Wong <djwong@kernel.org>
Signed-off-by: Darrick J. Wong <djwong@kernel.org>
2022-10-18 21:32:35 +00:00
|
|
|
if (isleaf1 && xfs_dir2_dataptr_to_db(geo,
|
|
|
|
be32_to_cpu(hdr->ents[i].address)) >=
|
|
|
|
be32_to_cpu(ltp->bestcount))
|
|
|
|
return __this_address;
|
2013-04-11 21:30:21 +00:00
|
|
|
}
|
|
|
|
if (hdr->stale != stale)
|
2018-01-08 18:51:03 +00:00
|
|
|
return __this_address;
|
|
|
|
return NULL;
|
2013-04-11 21:30:21 +00:00
|
|
|
}
|
|
|
|
|
2013-09-03 00:06:58 +00:00
|
|
|
/*
|
|
|
|
* We verify the magic numbers before decoding the leaf header so that on debug
|
|
|
|
* kernels we don't get assertion failures in xfs_dir3_leaf_hdr_from_disk() due
|
|
|
|
* to incorrect magic numbers.
|
|
|
|
*/
|
2018-01-08 18:51:03 +00:00
|
|
|
static xfs_failaddr_t
|
2013-04-11 21:30:21 +00:00
|
|
|
xfs_dir3_leaf_verify(
|
2019-11-08 23:06:03 +00:00
|
|
|
struct xfs_buf *bp)
|
2013-04-11 21:30:21 +00:00
|
|
|
{
|
2019-11-08 23:06:03 +00:00
|
|
|
struct xfs_mount *mp = bp->b_mount;
|
|
|
|
struct xfs_dir3_icleaf_hdr leafhdr;
|
|
|
|
xfs_failaddr_t fa;
|
2013-04-11 21:30:21 +00:00
|
|
|
|
2019-02-07 18:45:48 +00:00
|
|
|
fa = xfs_da3_blkinfo_verify(bp, bp->b_addr);
|
|
|
|
if (fa)
|
|
|
|
return fa;
|
2013-09-03 00:06:58 +00:00
|
|
|
|
2019-11-08 23:06:03 +00:00
|
|
|
xfs_dir2_leaf_hdr_from_disk(mp, &leafhdr, bp->b_addr);
|
2021-03-22 16:52:05 +00:00
|
|
|
return xfs_dir3_leaf_check_int(mp, &leafhdr, bp->b_addr, true);
|
2013-04-11 21:30:21 +00:00
|
|
|
}
|
|
|
|
|
2024-04-15 21:54:39 +00:00
|
|
|
xfs_failaddr_t
|
|
|
|
xfs_dir3_leaf_header_check(
|
|
|
|
struct xfs_buf *bp,
|
|
|
|
xfs_ino_t owner)
|
|
|
|
{
|
|
|
|
struct xfs_mount *mp = bp->b_mount;
|
|
|
|
|
|
|
|
if (xfs_has_crc(mp)) {
|
|
|
|
struct xfs_dir3_leaf *hdr3 = bp->b_addr;
|
|
|
|
|
|
|
|
if (hdr3->hdr.info.hdr.magic !=
|
|
|
|
cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) &&
|
|
|
|
hdr3->hdr.info.hdr.magic !=
|
|
|
|
cpu_to_be16(XFS_DIR3_LEAFN_MAGIC))
|
|
|
|
return __this_address;
|
|
|
|
|
|
|
|
if (be64_to_cpu(hdr3->hdr.info.owner) != owner)
|
|
|
|
return __this_address;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-04-11 21:30:21 +00:00
|
|
|
static void
|
2019-02-07 18:45:47 +00:00
|
|
|
xfs_dir3_leaf_read_verify(
|
|
|
|
struct xfs_buf *bp)
|
2013-04-11 21:30:21 +00:00
|
|
|
{
|
2019-06-29 02:27:29 +00:00
|
|
|
struct xfs_mount *mp = bp->b_mount;
|
2018-01-08 18:51:03 +00:00
|
|
|
xfs_failaddr_t fa;
|
2013-04-11 21:30:21 +00:00
|
|
|
|
2021-08-19 01:46:37 +00:00
|
|
|
if (xfs_has_crc(mp) &&
|
2014-02-27 04:23:10 +00:00
|
|
|
!xfs_buf_verify_cksum(bp, XFS_DIR3_LEAF_CRC_OFF))
|
2018-01-08 18:51:03 +00:00
|
|
|
xfs_verifier_error(bp, -EFSBADCRC, __this_address);
|
|
|
|
else {
|
2019-02-07 18:45:47 +00:00
|
|
|
fa = xfs_dir3_leaf_verify(bp);
|
2018-01-08 18:51:03 +00:00
|
|
|
if (fa)
|
|
|
|
xfs_verifier_error(bp, -EFSCORRUPTED, fa);
|
|
|
|
}
|
2013-04-11 21:30:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-02-07 18:45:47 +00:00
|
|
|
xfs_dir3_leaf_write_verify(
|
|
|
|
struct xfs_buf *bp)
|
2012-11-12 11:54:15 +00:00
|
|
|
{
|
2019-06-29 02:27:29 +00:00
|
|
|
struct xfs_mount *mp = bp->b_mount;
|
2018-01-24 21:38:48 +00:00
|
|
|
struct xfs_buf_log_item *bip = bp->b_log_item;
|
2013-04-11 21:30:21 +00:00
|
|
|
struct xfs_dir3_leaf_hdr *hdr3 = bp->b_addr;
|
2018-01-08 18:51:03 +00:00
|
|
|
xfs_failaddr_t fa;
|
2012-11-12 11:54:15 +00:00
|
|
|
|
2019-02-07 18:45:47 +00:00
|
|
|
fa = xfs_dir3_leaf_verify(bp);
|
2018-01-08 18:51:03 +00:00
|
|
|
if (fa) {
|
|
|
|
xfs_verifier_error(bp, -EFSCORRUPTED, fa);
|
2013-04-11 21:30:21 +00:00
|
|
|
return;
|
2012-11-12 11:54:15 +00:00
|
|
|
}
|
2013-04-11 21:30:21 +00:00
|
|
|
|
2021-08-19 01:46:37 +00:00
|
|
|
if (!xfs_has_crc(mp))
|
2013-04-11 21:30:21 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (bip)
|
|
|
|
hdr3->info.lsn = cpu_to_be64(bip->bli_item.li_lsn);
|
|
|
|
|
2014-02-27 04:18:23 +00:00
|
|
|
xfs_buf_update_cksum(bp, XFS_DIR3_LEAF_CRC_OFF);
|
2012-11-14 06:52:32 +00:00
|
|
|
}
|
2012-11-12 11:54:15 +00:00
|
|
|
|
2013-04-03 05:11:29 +00:00
|
|
|
const struct xfs_buf_ops xfs_dir3_leaf1_buf_ops = {
|
2016-01-04 05:10:19 +00:00
|
|
|
.name = "xfs_dir3_leaf1",
|
2019-02-16 19:47:28 +00:00
|
|
|
.magic16 = { cpu_to_be16(XFS_DIR2_LEAF1_MAGIC),
|
|
|
|
cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) },
|
2019-02-07 18:45:47 +00:00
|
|
|
.verify_read = xfs_dir3_leaf_read_verify,
|
|
|
|
.verify_write = xfs_dir3_leaf_write_verify,
|
|
|
|
.verify_struct = xfs_dir3_leaf_verify,
|
2012-11-14 06:54:40 +00:00
|
|
|
};
|
|
|
|
|
2013-04-11 21:30:21 +00:00
|
|
|
const struct xfs_buf_ops xfs_dir3_leafn_buf_ops = {
|
2016-01-04 05:10:19 +00:00
|
|
|
.name = "xfs_dir3_leafn",
|
2019-02-16 19:47:28 +00:00
|
|
|
.magic16 = { cpu_to_be16(XFS_DIR2_LEAFN_MAGIC),
|
|
|
|
cpu_to_be16(XFS_DIR3_LEAFN_MAGIC) },
|
2019-02-07 18:45:47 +00:00
|
|
|
.verify_read = xfs_dir3_leaf_read_verify,
|
|
|
|
.verify_write = xfs_dir3_leaf_write_verify,
|
|
|
|
.verify_struct = xfs_dir3_leaf_verify,
|
2012-11-14 06:54:40 +00:00
|
|
|
};
|
|
|
|
|
2017-06-16 18:00:07 +00:00
|
|
|
int
|
2013-04-11 21:30:21 +00:00
|
|
|
xfs_dir3_leaf_read(
|
2012-11-12 11:54:15 +00:00
|
|
|
struct xfs_trans *tp,
|
|
|
|
struct xfs_inode *dp,
|
2024-04-15 21:54:39 +00:00
|
|
|
xfs_ino_t owner,
|
2012-11-12 11:54:15 +00:00
|
|
|
xfs_dablk_t fbno,
|
|
|
|
struct xfs_buf **bpp)
|
|
|
|
{
|
2024-04-15 21:54:39 +00:00
|
|
|
xfs_failaddr_t fa;
|
2013-04-03 05:11:29 +00:00
|
|
|
int err;
|
|
|
|
|
2019-11-20 17:46:04 +00:00
|
|
|
err = xfs_da_read_buf(tp, dp, fbno, 0, bpp, XFS_DATA_FORK,
|
2019-11-20 17:46:03 +00:00
|
|
|
&xfs_dir3_leaf1_buf_ops);
|
2024-04-15 21:54:39 +00:00
|
|
|
if (err || !(*bpp))
|
|
|
|
return err;
|
|
|
|
|
|
|
|
fa = xfs_dir3_leaf_header_check(*bpp, owner);
|
|
|
|
if (fa) {
|
|
|
|
__xfs_buf_mark_corrupt(*bpp, fa);
|
|
|
|
xfs_trans_brelse(tp, *bpp);
|
|
|
|
*bpp = NULL;
|
|
|
|
xfs_dirattr_mark_sick(dp, XFS_DATA_FORK);
|
|
|
|
return -EFSCORRUPTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tp)
|
2013-04-03 05:11:30 +00:00
|
|
|
xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_LEAF1_BUF);
|
2024-04-15 21:54:39 +00:00
|
|
|
return 0;
|
2012-11-12 11:54:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2013-04-11 21:30:21 +00:00
|
|
|
xfs_dir3_leafn_read(
|
2012-11-12 11:54:15 +00:00
|
|
|
struct xfs_trans *tp,
|
|
|
|
struct xfs_inode *dp,
|
2024-04-15 21:54:39 +00:00
|
|
|
xfs_ino_t owner,
|
2012-11-12 11:54:15 +00:00
|
|
|
xfs_dablk_t fbno,
|
|
|
|
struct xfs_buf **bpp)
|
|
|
|
{
|
2024-04-15 21:54:39 +00:00
|
|
|
xfs_failaddr_t fa;
|
2013-04-03 05:11:29 +00:00
|
|
|
int err;
|
|
|
|
|
2019-11-20 17:46:04 +00:00
|
|
|
err = xfs_da_read_buf(tp, dp, fbno, 0, bpp, XFS_DATA_FORK,
|
2019-11-20 17:46:03 +00:00
|
|
|
&xfs_dir3_leafn_buf_ops);
|
2024-04-15 21:54:39 +00:00
|
|
|
if (err || !(*bpp))
|
|
|
|
return err;
|
|
|
|
|
|
|
|
fa = xfs_dir3_leaf_header_check(*bpp, owner);
|
|
|
|
if (fa) {
|
|
|
|
__xfs_buf_mark_corrupt(*bpp, fa);
|
|
|
|
xfs_trans_brelse(tp, *bpp);
|
|
|
|
*bpp = NULL;
|
|
|
|
xfs_dirattr_mark_sick(dp, XFS_DATA_FORK);
|
|
|
|
return -EFSCORRUPTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tp)
|
2013-04-03 05:11:30 +00:00
|
|
|
xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_LEAFN_BUF);
|
2024-04-15 21:54:39 +00:00
|
|
|
return 0;
|
2013-04-11 21:30:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize a new leaf block, leaf1 or leafn magic accepted.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
xfs_dir3_leaf_init(
|
2024-04-15 21:54:34 +00:00
|
|
|
struct xfs_da_args *args,
|
2013-04-11 21:30:21 +00:00
|
|
|
struct xfs_buf *bp,
|
2017-06-16 18:00:05 +00:00
|
|
|
uint16_t type)
|
2013-04-11 21:30:21 +00:00
|
|
|
{
|
2024-04-15 21:54:34 +00:00
|
|
|
struct xfs_mount *mp = args->dp->i_mount;
|
|
|
|
struct xfs_trans *tp = args->trans;
|
2013-04-11 21:30:21 +00:00
|
|
|
struct xfs_dir2_leaf *leaf = bp->b_addr;
|
|
|
|
|
|
|
|
ASSERT(type == XFS_DIR2_LEAF1_MAGIC || type == XFS_DIR2_LEAFN_MAGIC);
|
|
|
|
|
2021-08-19 01:46:37 +00:00
|
|
|
if (xfs_has_crc(mp)) {
|
2013-04-11 21:30:21 +00:00
|
|
|
struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
|
|
|
|
|
|
|
|
memset(leaf3, 0, sizeof(*leaf3));
|
|
|
|
|
|
|
|
leaf3->info.hdr.magic = (type == XFS_DIR2_LEAF1_MAGIC)
|
|
|
|
? cpu_to_be16(XFS_DIR3_LEAF1_MAGIC)
|
|
|
|
: cpu_to_be16(XFS_DIR3_LEAFN_MAGIC);
|
2021-08-19 01:47:05 +00:00
|
|
|
leaf3->info.blkno = cpu_to_be64(xfs_buf_daddr(bp));
|
2024-04-15 21:54:34 +00:00
|
|
|
leaf3->info.owner = cpu_to_be64(args->owner);
|
2015-07-29 01:53:31 +00:00
|
|
|
uuid_copy(&leaf3->info.uuid, &mp->m_sb.sb_meta_uuid);
|
2013-04-11 21:30:21 +00:00
|
|
|
} else {
|
|
|
|
memset(leaf, 0, sizeof(*leaf));
|
|
|
|
leaf->hdr.info.magic = cpu_to_be16(type);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If it's a leaf-format directory initialize the tail.
|
|
|
|
* Caller is responsible for initialising the bests table.
|
|
|
|
*/
|
|
|
|
if (type == XFS_DIR2_LEAF1_MAGIC) {
|
|
|
|
struct xfs_dir2_leaf_tail *ltp;
|
|
|
|
|
2014-06-06 05:15:59 +00:00
|
|
|
ltp = xfs_dir2_leaf_tail_p(mp->m_dir_geo, leaf);
|
2013-04-11 21:30:21 +00:00
|
|
|
ltp->bestcount = 0;
|
|
|
|
bp->b_ops = &xfs_dir3_leaf1_buf_ops;
|
2013-04-03 05:11:30 +00:00
|
|
|
xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_LEAF1_BUF);
|
2013-04-03 05:11:29 +00:00
|
|
|
} else {
|
2013-04-11 21:30:21 +00:00
|
|
|
bp->b_ops = &xfs_dir3_leafn_buf_ops;
|
2013-04-03 05:11:30 +00:00
|
|
|
xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_LEAFN_BUF);
|
2013-04-03 05:11:29 +00:00
|
|
|
}
|
2013-04-11 21:30:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
xfs_dir3_leaf_get_buf(
|
|
|
|
xfs_da_args_t *args,
|
|
|
|
xfs_dir2_db_t bno,
|
|
|
|
struct xfs_buf **bpp,
|
2017-06-16 18:00:05 +00:00
|
|
|
uint16_t magic)
|
2013-04-11 21:30:21 +00:00
|
|
|
{
|
|
|
|
struct xfs_inode *dp = args->dp;
|
|
|
|
struct xfs_trans *tp = args->trans;
|
|
|
|
struct xfs_buf *bp;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
ASSERT(magic == XFS_DIR2_LEAF1_MAGIC || magic == XFS_DIR2_LEAFN_MAGIC);
|
2014-06-06 05:08:18 +00:00
|
|
|
ASSERT(bno >= xfs_dir2_byte_to_db(args->geo, XFS_DIR2_LEAF_OFFSET) &&
|
|
|
|
bno < xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET));
|
2013-04-11 21:30:21 +00:00
|
|
|
|
2014-06-06 05:07:53 +00:00
|
|
|
error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(args->geo, bno),
|
2019-11-20 17:46:05 +00:00
|
|
|
&bp, XFS_DATA_FORK);
|
2013-04-11 21:30:21 +00:00
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
|
2024-04-15 21:54:34 +00:00
|
|
|
xfs_dir3_leaf_init(args, bp, magic);
|
2014-06-06 05:20:54 +00:00
|
|
|
xfs_dir3_leaf_log_header(args, bp);
|
2013-04-11 21:30:21 +00:00
|
|
|
if (magic == XFS_DIR2_LEAF1_MAGIC)
|
2014-06-06 05:20:54 +00:00
|
|
|
xfs_dir3_leaf_log_tail(args, bp);
|
2013-04-11 21:30:21 +00:00
|
|
|
*bpp = bp;
|
|
|
|
return 0;
|
2012-11-12 11:54:15 +00:00
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert a block form directory to a leaf form directory.
|
|
|
|
*/
|
|
|
|
int /* error */
|
|
|
|
xfs_dir2_block_to_leaf(
|
|
|
|
xfs_da_args_t *args, /* operation arguments */
|
2012-06-22 08:50:14 +00:00
|
|
|
struct xfs_buf *dbp) /* input block's buffer */
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2006-03-17 06:27:19 +00:00
|
|
|
__be16 *bestsp; /* leaf's bestsp entries */
|
2005-04-16 22:20:36 +00:00
|
|
|
xfs_dablk_t blkno; /* leaf block's bno */
|
2011-07-08 12:35:27 +00:00
|
|
|
xfs_dir2_data_hdr_t *hdr; /* block header */
|
2005-04-16 22:20:36 +00:00
|
|
|
xfs_dir2_leaf_entry_t *blp; /* block's leaf entries */
|
|
|
|
xfs_dir2_block_tail_t *btp; /* block's tail */
|
|
|
|
xfs_inode_t *dp; /* incore directory inode */
|
|
|
|
int error; /* error return code */
|
2012-06-22 08:50:14 +00:00
|
|
|
struct xfs_buf *lbp; /* leaf block's buffer */
|
2005-04-16 22:20:36 +00:00
|
|
|
xfs_dir2_db_t ldb; /* leaf block's bno */
|
|
|
|
xfs_dir2_leaf_t *leaf; /* leaf structure */
|
|
|
|
xfs_dir2_leaf_tail_t *ltp; /* leaf's tail */
|
|
|
|
int needlog; /* need to log block header */
|
|
|
|
int needscan; /* need to rescan bestfree */
|
|
|
|
xfs_trans_t *tp; /* transaction pointer */
|
2013-04-03 05:11:22 +00:00
|
|
|
struct xfs_dir2_data_free *bf;
|
2013-04-11 21:30:21 +00:00
|
|
|
struct xfs_dir3_icleaf_hdr leafhdr;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2009-12-14 23:14:59 +00:00
|
|
|
trace_xfs_dir2_block_to_leaf(args);
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
dp = args->dp;
|
|
|
|
tp = args->trans;
|
|
|
|
/*
|
|
|
|
* Add the leaf block to the inode.
|
|
|
|
* This interface will only put blocks in the leaf/node range.
|
|
|
|
* Since that's empty now, we'll get the root (block 0 in range).
|
|
|
|
*/
|
|
|
|
if ((error = xfs_da_grow_inode(args, &blkno))) {
|
|
|
|
return error;
|
|
|
|
}
|
2014-06-06 05:07:53 +00:00
|
|
|
ldb = xfs_dir2_da_to_db(args->geo, blkno);
|
2014-06-06 05:08:18 +00:00
|
|
|
ASSERT(ldb == xfs_dir2_byte_to_db(args->geo, XFS_DIR2_LEAF_OFFSET));
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* Initialize the leaf block, get a buffer for it.
|
|
|
|
*/
|
2013-04-11 21:30:21 +00:00
|
|
|
error = xfs_dir3_leaf_get_buf(args, ldb, &lbp, XFS_DIR2_LEAF1_MAGIC);
|
|
|
|
if (error)
|
2005-04-16 22:20:36 +00:00
|
|
|
return error;
|
2013-04-11 21:30:21 +00:00
|
|
|
|
2012-06-22 08:50:14 +00:00
|
|
|
leaf = lbp->b_addr;
|
|
|
|
hdr = dbp->b_addr;
|
2013-04-03 05:11:22 +00:00
|
|
|
xfs_dir3_data_check(dp, dbp);
|
2014-06-06 05:15:59 +00:00
|
|
|
btp = xfs_dir2_block_tail_p(args->geo, hdr);
|
2007-06-28 06:43:50 +00:00
|
|
|
blp = xfs_dir2_block_leaf_p(btp);
|
2019-11-08 23:05:39 +00:00
|
|
|
bf = xfs_dir2_data_bestfree_p(dp->i_mount, hdr);
|
2013-04-11 21:30:21 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* Set the counts in the leaf header.
|
|
|
|
*/
|
2019-11-08 22:57:49 +00:00
|
|
|
xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
|
2013-04-11 21:30:21 +00:00
|
|
|
leafhdr.count = be32_to_cpu(btp->count);
|
|
|
|
leafhdr.stale = be32_to_cpu(btp->stale);
|
2019-11-08 22:57:50 +00:00
|
|
|
xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf, &leafhdr);
|
2014-06-06 05:20:54 +00:00
|
|
|
xfs_dir3_leaf_log_header(args, lbp);
|
2013-04-11 21:30:21 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* Could compact these but I think we always do the conversion
|
|
|
|
* after squeezing out stale entries.
|
|
|
|
*/
|
2019-11-08 22:57:50 +00:00
|
|
|
memcpy(leafhdr.ents, blp,
|
|
|
|
be32_to_cpu(btp->count) * sizeof(struct xfs_dir2_leaf_entry));
|
|
|
|
xfs_dir3_leaf_log_ents(args, &leafhdr, lbp, 0, leafhdr.count - 1);
|
2005-04-16 22:20:36 +00:00
|
|
|
needscan = 0;
|
|
|
|
needlog = 1;
|
|
|
|
/*
|
|
|
|
* Make the space formerly occupied by the leaf entries and block
|
|
|
|
* tail be free.
|
|
|
|
*/
|
2014-06-06 05:20:54 +00:00
|
|
|
xfs_dir2_data_make_free(args, dbp,
|
2011-07-08 12:35:27 +00:00
|
|
|
(xfs_dir2_data_aoff_t)((char *)blp - (char *)hdr),
|
2014-06-06 05:15:59 +00:00
|
|
|
(xfs_dir2_data_aoff_t)((char *)hdr + args->geo->blksize -
|
2005-04-16 22:20:36 +00:00
|
|
|
(char *)blp),
|
|
|
|
&needlog, &needscan);
|
|
|
|
/*
|
|
|
|
* Fix up the block header, make it a data block.
|
|
|
|
*/
|
2013-04-03 05:11:22 +00:00
|
|
|
dbp->b_ops = &xfs_dir3_data_buf_ops;
|
2013-04-03 05:11:30 +00:00
|
|
|
xfs_trans_buf_set_type(tp, dbp, XFS_BLFT_DIR_DATA_BUF);
|
2013-04-03 05:11:22 +00:00
|
|
|
if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC))
|
|
|
|
hdr->magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
|
|
|
|
else
|
|
|
|
hdr->magic = cpu_to_be32(XFS_DIR3_DATA_MAGIC);
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
if (needscan)
|
2019-11-08 23:06:02 +00:00
|
|
|
xfs_dir2_data_freescan(dp->i_mount, hdr, &needlog);
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* Set up leaf tail and bests table.
|
|
|
|
*/
|
2014-06-06 05:15:59 +00:00
|
|
|
ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
|
2006-03-17 06:27:28 +00:00
|
|
|
ltp->bestcount = cpu_to_be32(1);
|
2007-06-28 06:43:50 +00:00
|
|
|
bestsp = xfs_dir2_leaf_bests_p(ltp);
|
2013-04-03 05:11:20 +00:00
|
|
|
bestsp[0] = bf[0].length;
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* Log the data header and leaf bests table.
|
|
|
|
*/
|
|
|
|
if (needlog)
|
2014-06-06 05:20:54 +00:00
|
|
|
xfs_dir2_data_log_header(args, dbp);
|
2013-10-29 11:11:50 +00:00
|
|
|
xfs_dir3_leaf_check(dp, lbp);
|
2013-04-03 05:11:22 +00:00
|
|
|
xfs_dir3_data_check(dp, dbp);
|
2014-06-06 05:20:54 +00:00
|
|
|
xfs_dir3_leaf_log_bests(args, lbp, 0, 0);
|
2005-04-16 22:20:36 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-07-13 11:43:48 +00:00
|
|
|
STATIC void
|
2013-04-11 21:30:21 +00:00
|
|
|
xfs_dir3_leaf_find_stale(
|
|
|
|
struct xfs_dir3_icleaf_hdr *leafhdr,
|
|
|
|
struct xfs_dir2_leaf_entry *ents,
|
2011-07-13 11:43:48 +00:00
|
|
|
int index,
|
|
|
|
int *lowstale,
|
|
|
|
int *highstale)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Find the first stale entry before our index, if any.
|
|
|
|
*/
|
|
|
|
for (*lowstale = index - 1; *lowstale >= 0; --*lowstale) {
|
2013-04-11 21:30:21 +00:00
|
|
|
if (ents[*lowstale].address ==
|
2011-07-13 11:43:48 +00:00
|
|
|
cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find the first stale entry at or after our index, if any.
|
|
|
|
* Stop if the result would require moving more entries than using
|
|
|
|
* lowstale.
|
|
|
|
*/
|
2013-04-11 21:30:21 +00:00
|
|
|
for (*highstale = index; *highstale < leafhdr->count; ++*highstale) {
|
|
|
|
if (ents[*highstale].address ==
|
2011-07-13 11:43:48 +00:00
|
|
|
cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
|
|
|
|
break;
|
|
|
|
if (*lowstale >= 0 && index - *lowstale <= *highstale - index)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-08 12:34:59 +00:00
|
|
|
struct xfs_dir2_leaf_entry *
|
2013-04-11 21:30:21 +00:00
|
|
|
xfs_dir3_leaf_find_entry(
|
|
|
|
struct xfs_dir3_icleaf_hdr *leafhdr,
|
|
|
|
struct xfs_dir2_leaf_entry *ents,
|
2011-07-08 12:34:59 +00:00
|
|
|
int index, /* leaf table position */
|
|
|
|
int compact, /* need to compact leaves */
|
|
|
|
int lowstale, /* index of prev stale leaf */
|
|
|
|
int highstale, /* index of next stale leaf */
|
|
|
|
int *lfloglow, /* low leaf logging index */
|
|
|
|
int *lfloghigh) /* high leaf logging index */
|
|
|
|
{
|
2013-04-11 21:30:21 +00:00
|
|
|
if (!leafhdr->stale) {
|
2011-07-08 12:34:59 +00:00
|
|
|
xfs_dir2_leaf_entry_t *lep; /* leaf entry table pointer */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now we need to make room to insert the leaf entry.
|
|
|
|
*
|
|
|
|
* If there are no stale entries, just insert a hole at index.
|
|
|
|
*/
|
2013-04-11 21:30:21 +00:00
|
|
|
lep = &ents[index];
|
|
|
|
if (index < leafhdr->count)
|
2011-07-08 12:34:59 +00:00
|
|
|
memmove(lep + 1, lep,
|
2013-04-11 21:30:21 +00:00
|
|
|
(leafhdr->count - index) * sizeof(*lep));
|
2011-07-08 12:34:59 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Record low and high logging indices for the leaf.
|
|
|
|
*/
|
|
|
|
*lfloglow = index;
|
2013-04-11 21:30:21 +00:00
|
|
|
*lfloghigh = leafhdr->count++;
|
2011-07-08 12:34:59 +00:00
|
|
|
return lep;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* There are stale entries.
|
|
|
|
*
|
|
|
|
* We will use one of them for the new entry. It's probably not at
|
|
|
|
* the right location, so we'll have to shift some up or down first.
|
|
|
|
*
|
|
|
|
* If we didn't compact before, we need to find the nearest stale
|
|
|
|
* entries before and after our insertion point.
|
|
|
|
*/
|
2011-07-13 11:43:48 +00:00
|
|
|
if (compact == 0)
|
2013-04-11 21:30:21 +00:00
|
|
|
xfs_dir3_leaf_find_stale(leafhdr, ents, index,
|
|
|
|
&lowstale, &highstale);
|
2011-07-08 12:34:59 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If the low one is better, use it.
|
|
|
|
*/
|
|
|
|
if (lowstale >= 0 &&
|
2013-04-11 21:30:21 +00:00
|
|
|
(highstale == leafhdr->count ||
|
2011-07-08 12:34:59 +00:00
|
|
|
index - lowstale - 1 < highstale - index)) {
|
|
|
|
ASSERT(index - lowstale - 1 >= 0);
|
2013-04-11 21:30:21 +00:00
|
|
|
ASSERT(ents[lowstale].address ==
|
2011-07-08 12:36:05 +00:00
|
|
|
cpu_to_be32(XFS_DIR2_NULL_DATAPTR));
|
2011-07-08 12:34:59 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copy entries up to cover the stale entry and make room
|
|
|
|
* for the new entry.
|
|
|
|
*/
|
|
|
|
if (index - lowstale - 1 > 0) {
|
2013-04-11 21:30:21 +00:00
|
|
|
memmove(&ents[lowstale], &ents[lowstale + 1],
|
2011-07-08 12:34:59 +00:00
|
|
|
(index - lowstale - 1) *
|
2013-04-11 21:30:21 +00:00
|
|
|
sizeof(xfs_dir2_leaf_entry_t));
|
2011-07-08 12:34:59 +00:00
|
|
|
}
|
2018-06-07 14:54:02 +00:00
|
|
|
*lfloglow = min(lowstale, *lfloglow);
|
|
|
|
*lfloghigh = max(index - 1, *lfloghigh);
|
2013-04-11 21:30:21 +00:00
|
|
|
leafhdr->stale--;
|
|
|
|
return &ents[index - 1];
|
2011-07-08 12:34:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The high one is better, so use that one.
|
|
|
|
*/
|
|
|
|
ASSERT(highstale - index >= 0);
|
2013-04-11 21:30:21 +00:00
|
|
|
ASSERT(ents[highstale].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR));
|
2011-07-08 12:34:59 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copy entries down to cover the stale entry and make room for the
|
|
|
|
* new entry.
|
|
|
|
*/
|
|
|
|
if (highstale - index > 0) {
|
2013-04-11 21:30:21 +00:00
|
|
|
memmove(&ents[index + 1], &ents[index],
|
2011-07-08 12:34:59 +00:00
|
|
|
(highstale - index) * sizeof(xfs_dir2_leaf_entry_t));
|
|
|
|
}
|
2018-06-07 14:54:02 +00:00
|
|
|
*lfloglow = min(index, *lfloglow);
|
|
|
|
*lfloghigh = max(highstale, *lfloghigh);
|
2013-04-11 21:30:21 +00:00
|
|
|
leafhdr->stale--;
|
|
|
|
return &ents[index];
|
2011-07-08 12:34:59 +00:00
|
|
|
}
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* Add an entry to a leaf form directory.
|
|
|
|
*/
|
|
|
|
int /* error */
|
|
|
|
xfs_dir2_leaf_addname(
|
2019-03-10 18:46:28 +00:00
|
|
|
struct xfs_da_args *args) /* operation arguments */
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2019-03-10 18:46:28 +00:00
|
|
|
struct xfs_dir3_icleaf_hdr leafhdr;
|
|
|
|
struct xfs_trans *tp = args->trans;
|
2006-03-17 06:27:19 +00:00
|
|
|
__be16 *bestsp; /* freespace table in leaf */
|
2019-03-10 18:46:28 +00:00
|
|
|
__be16 *tagp; /* end of data entry */
|
2012-06-22 08:50:14 +00:00
|
|
|
struct xfs_buf *dbp; /* data block buffer */
|
2019-03-10 18:46:28 +00:00
|
|
|
struct xfs_buf *lbp; /* leaf's buffer */
|
|
|
|
struct xfs_dir2_leaf *leaf; /* leaf structure */
|
|
|
|
struct xfs_inode *dp = args->dp; /* incore directory inode */
|
|
|
|
struct xfs_dir2_data_hdr *hdr; /* data block header */
|
|
|
|
struct xfs_dir2_data_entry *dep; /* data block entry */
|
|
|
|
struct xfs_dir2_leaf_entry *lep; /* leaf entry table pointer */
|
|
|
|
struct xfs_dir2_leaf_entry *ents;
|
|
|
|
struct xfs_dir2_data_unused *dup; /* data unused entry */
|
|
|
|
struct xfs_dir2_leaf_tail *ltp; /* leaf tail pointer */
|
|
|
|
struct xfs_dir2_data_free *bf; /* bestfree table */
|
|
|
|
int compact; /* need to compact leaves */
|
2005-04-16 22:20:36 +00:00
|
|
|
int error; /* error return value */
|
|
|
|
int grown; /* allocated new data block */
|
2019-03-10 18:41:31 +00:00
|
|
|
int highstale = 0; /* index of next stale leaf */
|
2005-04-16 22:20:36 +00:00
|
|
|
int i; /* temporary, index */
|
|
|
|
int index; /* leaf table position */
|
|
|
|
int length; /* length of new entry */
|
|
|
|
int lfloglow; /* low leaf logging index */
|
|
|
|
int lfloghigh; /* high leaf logging index */
|
2019-03-10 18:41:31 +00:00
|
|
|
int lowstale = 0; /* index of prev stale leaf */
|
2005-04-16 22:20:36 +00:00
|
|
|
int needbytes; /* leaf block bytes needed */
|
|
|
|
int needlog; /* need to log data header */
|
|
|
|
int needscan; /* need to rescan data free */
|
|
|
|
xfs_dir2_db_t use_block; /* data block number */
|
|
|
|
|
2009-12-14 23:14:59 +00:00
|
|
|
trace_xfs_dir2_leaf_addname(args);
|
|
|
|
|
2024-04-15 21:54:39 +00:00
|
|
|
error = xfs_dir3_leaf_read(tp, dp, args->owner, args->geo->leafblk,
|
|
|
|
&lbp);
|
2012-11-12 11:54:10 +00:00
|
|
|
if (error)
|
2005-04-16 22:20:36 +00:00
|
|
|
return error;
|
2012-11-12 11:54:15 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* Look up the entry by hash value and name.
|
|
|
|
* We know it's not there, our caller has already done a lookup.
|
|
|
|
* So the index is of the entry to insert in front of.
|
|
|
|
* But if there are dup hash values the index is of the first of those.
|
|
|
|
*/
|
|
|
|
index = xfs_dir2_leaf_search_hash(args, lbp);
|
2012-06-22 08:50:14 +00:00
|
|
|
leaf = lbp->b_addr;
|
2014-06-06 05:15:59 +00:00
|
|
|
ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
|
2019-11-08 22:57:49 +00:00
|
|
|
xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
|
2019-11-08 22:57:50 +00:00
|
|
|
ents = leafhdr.ents;
|
2007-06-28 06:43:50 +00:00
|
|
|
bestsp = xfs_dir2_leaf_bests_p(ltp);
|
2019-11-08 23:05:37 +00:00
|
|
|
length = xfs_dir2_data_entsize(dp->i_mount, args->namelen);
|
2013-04-11 21:30:21 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* See if there are any entries with the same hash value
|
|
|
|
* and space in their block for the new entry.
|
|
|
|
* This is good because it puts multiple same-hash value entries
|
|
|
|
* in a data block, improving the lookup of those entries.
|
|
|
|
*/
|
2013-04-11 21:30:21 +00:00
|
|
|
for (use_block = -1, lep = &ents[index];
|
|
|
|
index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
|
2005-04-16 22:20:36 +00:00
|
|
|
index++, lep++) {
|
2006-03-17 06:28:18 +00:00
|
|
|
if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
|
2005-04-16 22:20:36 +00:00
|
|
|
continue;
|
2014-06-06 05:08:18 +00:00
|
|
|
i = xfs_dir2_dataptr_to_db(args->geo, be32_to_cpu(lep->address));
|
2006-03-17 06:27:28 +00:00
|
|
|
ASSERT(i < be32_to_cpu(ltp->bestcount));
|
2011-07-08 12:36:05 +00:00
|
|
|
ASSERT(bestsp[i] != cpu_to_be16(NULLDATAOFF));
|
2006-03-17 06:27:19 +00:00
|
|
|
if (be16_to_cpu(bestsp[i]) >= length) {
|
2005-04-16 22:20:36 +00:00
|
|
|
use_block = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Didn't find a block yet, linear search all the data blocks.
|
|
|
|
*/
|
|
|
|
if (use_block == -1) {
|
2006-03-17 06:27:28 +00:00
|
|
|
for (i = 0; i < be32_to_cpu(ltp->bestcount); i++) {
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* Remember a block we see that's missing.
|
|
|
|
*/
|
2011-07-08 12:36:05 +00:00
|
|
|
if (bestsp[i] == cpu_to_be16(NULLDATAOFF) &&
|
|
|
|
use_block == -1)
|
2005-04-16 22:20:36 +00:00
|
|
|
use_block = i;
|
2006-03-17 06:27:19 +00:00
|
|
|
else if (be16_to_cpu(bestsp[i]) >= length) {
|
2005-04-16 22:20:36 +00:00
|
|
|
use_block = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* How many bytes do we need in the leaf block?
|
|
|
|
*/
|
2011-07-08 12:35:53 +00:00
|
|
|
needbytes = 0;
|
2013-04-11 21:30:21 +00:00
|
|
|
if (!leafhdr.stale)
|
2011-07-08 12:35:53 +00:00
|
|
|
needbytes += sizeof(xfs_dir2_leaf_entry_t);
|
|
|
|
if (use_block == -1)
|
|
|
|
needbytes += sizeof(xfs_dir2_data_off_t);
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* Now kill use_block if it refers to a missing block, so we
|
|
|
|
* can use it as an indication of allocation needed.
|
|
|
|
*/
|
2011-07-08 12:36:05 +00:00
|
|
|
if (use_block != -1 && bestsp[use_block] == cpu_to_be16(NULLDATAOFF))
|
2005-04-16 22:20:36 +00:00
|
|
|
use_block = -1;
|
|
|
|
/*
|
|
|
|
* If we don't have enough free bytes but we can make enough
|
|
|
|
* by compacting out stale entries, we'll do that.
|
|
|
|
*/
|
2013-04-11 21:30:21 +00:00
|
|
|
if ((char *)bestsp - (char *)&ents[leafhdr.count] < needbytes &&
|
|
|
|
leafhdr.stale > 1)
|
2005-04-16 22:20:36 +00:00
|
|
|
compact = 1;
|
2013-04-11 21:30:21 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* Otherwise if we don't have enough free bytes we need to
|
|
|
|
* convert to node form.
|
|
|
|
*/
|
2013-04-11 21:30:21 +00:00
|
|
|
else if ((char *)bestsp - (char *)&ents[leafhdr.count] < needbytes) {
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* Just checking or no space reservation, give up.
|
|
|
|
*/
|
2008-05-21 06:42:05 +00:00
|
|
|
if ((args->op_flags & XFS_DA_OP_JUSTCHECK) ||
|
|
|
|
args->total == 0) {
|
2012-06-22 08:50:14 +00:00
|
|
|
xfs_trans_brelse(tp, lbp);
|
2014-06-25 04:58:08 +00:00
|
|
|
return -ENOSPC;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Convert to node form.
|
|
|
|
*/
|
|
|
|
error = xfs_dir2_leaf_to_node(args, lbp);
|
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
/*
|
|
|
|
* Then add the new entry.
|
|
|
|
*/
|
|
|
|
return xfs_dir2_node_addname(args);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Otherwise it will fit without compaction.
|
|
|
|
*/
|
|
|
|
else
|
|
|
|
compact = 0;
|
|
|
|
/*
|
|
|
|
* If just checking, then it will fit unless we needed to allocate
|
|
|
|
* a new data block.
|
|
|
|
*/
|
2008-05-21 06:42:05 +00:00
|
|
|
if (args->op_flags & XFS_DA_OP_JUSTCHECK) {
|
2012-06-22 08:50:14 +00:00
|
|
|
xfs_trans_brelse(tp, lbp);
|
2014-06-25 04:58:08 +00:00
|
|
|
return use_block == -1 ? -ENOSPC : 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* If no allocations are allowed, return now before we've
|
|
|
|
* changed anything.
|
|
|
|
*/
|
|
|
|
if (args->total == 0 && use_block == -1) {
|
2012-06-22 08:50:14 +00:00
|
|
|
xfs_trans_brelse(tp, lbp);
|
2014-06-25 04:58:08 +00:00
|
|
|
return -ENOSPC;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Need to compact the leaf entries, removing stale ones.
|
|
|
|
* Leave one stale entry behind - the one closest to our
|
|
|
|
* insertion index - and we'll shift that one to our insertion
|
|
|
|
* point later.
|
|
|
|
*/
|
|
|
|
if (compact) {
|
2013-04-11 21:30:21 +00:00
|
|
|
xfs_dir3_leaf_compact_x1(&leafhdr, ents, &index, &lowstale,
|
|
|
|
&highstale, &lfloglow, &lfloghigh);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* There are stale entries, so we'll need log-low and log-high
|
|
|
|
* impossibly bad values later.
|
|
|
|
*/
|
2013-04-11 21:30:21 +00:00
|
|
|
else if (leafhdr.stale) {
|
|
|
|
lfloglow = leafhdr.count;
|
2005-04-16 22:20:36 +00:00
|
|
|
lfloghigh = -1;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* If there was no data block space found, we need to allocate
|
|
|
|
* a new one.
|
|
|
|
*/
|
|
|
|
if (use_block == -1) {
|
|
|
|
/*
|
|
|
|
* Add the new data block.
|
|
|
|
*/
|
|
|
|
if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE,
|
|
|
|
&use_block))) {
|
2012-06-22 08:50:14 +00:00
|
|
|
xfs_trans_brelse(tp, lbp);
|
2005-04-16 22:20:36 +00:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Initialize the block.
|
|
|
|
*/
|
2013-04-03 05:11:20 +00:00
|
|
|
if ((error = xfs_dir3_data_init(args, use_block, &dbp))) {
|
2012-06-22 08:50:14 +00:00
|
|
|
xfs_trans_brelse(tp, lbp);
|
2005-04-16 22:20:36 +00:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* If we're adding a new data block on the end we need to
|
|
|
|
* extend the bests table. Copy it up one entry.
|
|
|
|
*/
|
2006-03-17 06:27:28 +00:00
|
|
|
if (use_block >= be32_to_cpu(ltp->bestcount)) {
|
2005-04-16 22:20:36 +00:00
|
|
|
bestsp--;
|
|
|
|
memmove(&bestsp[0], &bestsp[1],
|
2006-03-17 06:27:28 +00:00
|
|
|
be32_to_cpu(ltp->bestcount) * sizeof(bestsp[0]));
|
2008-02-13 23:03:29 +00:00
|
|
|
be32_add_cpu(<p->bestcount, 1);
|
2014-06-06 05:20:54 +00:00
|
|
|
xfs_dir3_leaf_log_tail(args, lbp);
|
|
|
|
xfs_dir3_leaf_log_bests(args, lbp, 0,
|
|
|
|
be32_to_cpu(ltp->bestcount) - 1);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* If we're filling in a previously empty block just log it.
|
|
|
|
*/
|
|
|
|
else
|
2014-06-06 05:20:54 +00:00
|
|
|
xfs_dir3_leaf_log_bests(args, lbp, use_block, use_block);
|
2012-06-22 08:50:14 +00:00
|
|
|
hdr = dbp->b_addr;
|
2019-11-08 23:05:39 +00:00
|
|
|
bf = xfs_dir2_data_bestfree_p(dp->i_mount, hdr);
|
2013-04-03 05:11:22 +00:00
|
|
|
bestsp[use_block] = bf[0].length;
|
2005-04-16 22:20:36 +00:00
|
|
|
grown = 1;
|
2012-11-12 11:54:14 +00:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Already had space in some data block.
|
|
|
|
* Just read that one in.
|
|
|
|
*/
|
2024-04-15 21:54:40 +00:00
|
|
|
error = xfs_dir3_data_read(tp, dp, args->owner,
|
|
|
|
xfs_dir2_db_to_da(args->geo, use_block), 0,
|
|
|
|
&dbp);
|
2012-11-12 11:54:10 +00:00
|
|
|
if (error) {
|
2012-06-22 08:50:14 +00:00
|
|
|
xfs_trans_brelse(tp, lbp);
|
2005-04-16 22:20:36 +00:00
|
|
|
return error;
|
|
|
|
}
|
2012-06-22 08:50:14 +00:00
|
|
|
hdr = dbp->b_addr;
|
2019-11-08 23:05:39 +00:00
|
|
|
bf = xfs_dir2_data_bestfree_p(dp->i_mount, hdr);
|
2005-04-16 22:20:36 +00:00
|
|
|
grown = 0;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Point to the biggest freespace in our data block.
|
|
|
|
*/
|
|
|
|
dup = (xfs_dir2_data_unused_t *)
|
2013-04-03 05:11:22 +00:00
|
|
|
((char *)hdr + be16_to_cpu(bf[0].offset));
|
2005-04-16 22:20:36 +00:00
|
|
|
needscan = needlog = 0;
|
|
|
|
/*
|
|
|
|
* Mark the initial part of our freespace in use for the new entry.
|
|
|
|
*/
|
2018-03-23 17:06:51 +00:00
|
|
|
error = xfs_dir2_data_use_free(args, dbp, dup,
|
|
|
|
(xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr),
|
|
|
|
length, &needlog, &needscan);
|
|
|
|
if (error) {
|
|
|
|
xfs_trans_brelse(tp, lbp);
|
|
|
|
return error;
|
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* Initialize our new entry (at last).
|
|
|
|
*/
|
|
|
|
dep = (xfs_dir2_data_entry_t *)dup;
|
2006-06-09 04:48:37 +00:00
|
|
|
dep->inumber = cpu_to_be64(args->inumber);
|
2005-04-16 22:20:36 +00:00
|
|
|
dep->namelen = args->namelen;
|
|
|
|
memcpy(dep->name, args->name, dep->namelen);
|
2019-11-08 23:05:48 +00:00
|
|
|
xfs_dir2_data_put_ftype(dp->i_mount, dep, args->filetype);
|
2019-11-08 23:05:37 +00:00
|
|
|
tagp = xfs_dir2_data_entry_tag_p(dp->i_mount, dep);
|
2011-07-08 12:35:38 +00:00
|
|
|
*tagp = cpu_to_be16((char *)dep - (char *)hdr);
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* Need to scan fix up the bestfree table.
|
|
|
|
*/
|
|
|
|
if (needscan)
|
2019-11-08 23:06:02 +00:00
|
|
|
xfs_dir2_data_freescan(dp->i_mount, hdr, &needlog);
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* Need to log the data block's header.
|
|
|
|
*/
|
|
|
|
if (needlog)
|
2014-06-06 05:20:54 +00:00
|
|
|
xfs_dir2_data_log_header(args, dbp);
|
|
|
|
xfs_dir2_data_log_entry(args, dbp, dep);
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* If the bests table needs to be changed, do it.
|
|
|
|
* Log the change unless we've already done that.
|
|
|
|
*/
|
2013-04-03 05:11:22 +00:00
|
|
|
if (be16_to_cpu(bestsp[use_block]) != be16_to_cpu(bf[0].length)) {
|
|
|
|
bestsp[use_block] = bf[0].length;
|
2005-04-16 22:20:36 +00:00
|
|
|
if (!grown)
|
2014-06-06 05:20:54 +00:00
|
|
|
xfs_dir3_leaf_log_bests(args, lbp, use_block, use_block);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2011-07-08 12:34:59 +00:00
|
|
|
|
2013-04-11 21:30:21 +00:00
|
|
|
lep = xfs_dir3_leaf_find_entry(&leafhdr, ents, index, compact, lowstale,
|
2011-07-08 12:34:59 +00:00
|
|
|
highstale, &lfloglow, &lfloghigh);
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* Fill in the new leaf entry.
|
|
|
|
*/
|
2006-03-17 06:28:18 +00:00
|
|
|
lep->hashval = cpu_to_be32(args->hashval);
|
2014-06-06 05:08:18 +00:00
|
|
|
lep->address = cpu_to_be32(
|
|
|
|
xfs_dir2_db_off_to_dataptr(args->geo, use_block,
|
2006-03-17 06:28:27 +00:00
|
|
|
be16_to_cpu(*tagp)));
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* Log the leaf fields and give up the buffers.
|
|
|
|
*/
|
2019-11-08 22:57:50 +00:00
|
|
|
xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf, &leafhdr);
|
2014-06-06 05:20:54 +00:00
|
|
|
xfs_dir3_leaf_log_header(args, lbp);
|
2019-11-08 22:57:50 +00:00
|
|
|
xfs_dir3_leaf_log_ents(args, &leafhdr, lbp, lfloglow, lfloghigh);
|
2013-10-29 11:11:50 +00:00
|
|
|
xfs_dir3_leaf_check(dp, lbp);
|
2013-04-03 05:11:22 +00:00
|
|
|
xfs_dir3_data_check(dp, dbp);
|
2005-04-16 22:20:36 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Compact out any stale entries in the leaf.
|
|
|
|
* Log the header and changed leaf entries, if any.
|
|
|
|
*/
|
|
|
|
void
|
2013-04-11 21:30:21 +00:00
|
|
|
xfs_dir3_leaf_compact(
|
2005-04-16 22:20:36 +00:00
|
|
|
xfs_da_args_t *args, /* operation arguments */
|
2013-04-11 21:30:21 +00:00
|
|
|
struct xfs_dir3_icleaf_hdr *leafhdr,
|
2012-06-22 08:50:14 +00:00
|
|
|
struct xfs_buf *bp) /* leaf buffer */
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
int from; /* source leaf index */
|
|
|
|
xfs_dir2_leaf_t *leaf; /* leaf structure */
|
|
|
|
int loglow; /* first leaf entry to log */
|
|
|
|
int to; /* target leaf index */
|
2013-10-29 11:11:52 +00:00
|
|
|
struct xfs_inode *dp = args->dp;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2012-06-22 08:50:14 +00:00
|
|
|
leaf = bp->b_addr;
|
2013-04-11 21:30:21 +00:00
|
|
|
if (!leafhdr->stale)
|
2005-04-16 22:20:36 +00:00
|
|
|
return;
|
2013-04-11 21:30:21 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* Compress out the stale entries in place.
|
|
|
|
*/
|
2013-04-11 21:30:21 +00:00
|
|
|
for (from = to = 0, loglow = -1; from < leafhdr->count; from++) {
|
2019-11-08 22:57:50 +00:00
|
|
|
if (leafhdr->ents[from].address ==
|
|
|
|
cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
|
2005-04-16 22:20:36 +00:00
|
|
|
continue;
|
|
|
|
/*
|
|
|
|
* Only actually copy the entries that are different.
|
|
|
|
*/
|
|
|
|
if (from > to) {
|
|
|
|
if (loglow == -1)
|
|
|
|
loglow = to;
|
2019-11-08 22:57:50 +00:00
|
|
|
leafhdr->ents[to] = leafhdr->ents[from];
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
to++;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Update and log the header, log the leaf entries.
|
|
|
|
*/
|
2013-04-11 21:30:21 +00:00
|
|
|
ASSERT(leafhdr->stale == from - to);
|
|
|
|
leafhdr->count -= leafhdr->stale;
|
|
|
|
leafhdr->stale = 0;
|
|
|
|
|
2019-11-08 22:57:50 +00:00
|
|
|
xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf, leafhdr);
|
2014-06-06 05:20:54 +00:00
|
|
|
xfs_dir3_leaf_log_header(args, bp);
|
2005-04-16 22:20:36 +00:00
|
|
|
if (loglow != -1)
|
2019-11-08 22:57:50 +00:00
|
|
|
xfs_dir3_leaf_log_ents(args, leafhdr, bp, loglow, to - 1);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Compact the leaf entries, removing stale ones.
|
|
|
|
* Leave one stale entry behind - the one closest to our
|
|
|
|
* insertion index - and the caller will shift that one to our insertion
|
|
|
|
* point later.
|
|
|
|
* Return new insertion index, where the remaining stale entry is,
|
|
|
|
* and leaf logging indices.
|
|
|
|
*/
|
|
|
|
void
|
2013-04-11 21:30:21 +00:00
|
|
|
xfs_dir3_leaf_compact_x1(
|
|
|
|
struct xfs_dir3_icleaf_hdr *leafhdr,
|
|
|
|
struct xfs_dir2_leaf_entry *ents,
|
2005-04-16 22:20:36 +00:00
|
|
|
int *indexp, /* insertion index */
|
|
|
|
int *lowstalep, /* out: stale entry before us */
|
|
|
|
int *highstalep, /* out: stale entry after us */
|
|
|
|
int *lowlogp, /* out: low log index */
|
|
|
|
int *highlogp) /* out: high log index */
|
|
|
|
{
|
|
|
|
int from; /* source copy index */
|
|
|
|
int highstale; /* stale entry at/after index */
|
|
|
|
int index; /* insertion index */
|
|
|
|
int keepstale; /* source index of kept stale */
|
|
|
|
int lowstale; /* stale entry before index */
|
|
|
|
int newindex=0; /* new insertion index */
|
|
|
|
int to; /* destination copy index */
|
|
|
|
|
2013-04-11 21:30:21 +00:00
|
|
|
ASSERT(leafhdr->stale > 1);
|
2005-04-16 22:20:36 +00:00
|
|
|
index = *indexp;
|
2011-07-13 11:43:48 +00:00
|
|
|
|
2013-04-11 21:30:21 +00:00
|
|
|
xfs_dir3_leaf_find_stale(leafhdr, ents, index, &lowstale, &highstale);
|
2011-07-13 11:43:48 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* Pick the better of lowstale and highstale.
|
|
|
|
*/
|
|
|
|
if (lowstale >= 0 &&
|
2013-04-11 21:30:21 +00:00
|
|
|
(highstale == leafhdr->count ||
|
2005-04-16 22:20:36 +00:00
|
|
|
index - lowstale <= highstale - index))
|
|
|
|
keepstale = lowstale;
|
|
|
|
else
|
|
|
|
keepstale = highstale;
|
|
|
|
/*
|
|
|
|
* Copy the entries in place, removing all the stale entries
|
|
|
|
* except keepstale.
|
|
|
|
*/
|
2013-04-11 21:30:21 +00:00
|
|
|
for (from = to = 0; from < leafhdr->count; from++) {
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* Notice the new value of index.
|
|
|
|
*/
|
|
|
|
if (index == from)
|
|
|
|
newindex = to;
|
|
|
|
if (from != keepstale &&
|
2013-04-11 21:30:21 +00:00
|
|
|
ents[from].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) {
|
2005-04-16 22:20:36 +00:00
|
|
|
if (from == to)
|
|
|
|
*lowlogp = to;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Record the new keepstale value for the insertion.
|
|
|
|
*/
|
|
|
|
if (from == keepstale)
|
|
|
|
lowstale = highstale = to;
|
|
|
|
/*
|
|
|
|
* Copy only the entries that have moved.
|
|
|
|
*/
|
|
|
|
if (from > to)
|
2013-04-11 21:30:21 +00:00
|
|
|
ents[to] = ents[from];
|
2005-04-16 22:20:36 +00:00
|
|
|
to++;
|
|
|
|
}
|
|
|
|
ASSERT(from > to);
|
|
|
|
/*
|
|
|
|
* If the insertion point was past the last entry,
|
|
|
|
* set the new insertion point accordingly.
|
|
|
|
*/
|
|
|
|
if (index == from)
|
|
|
|
newindex = to;
|
|
|
|
*indexp = newindex;
|
|
|
|
/*
|
|
|
|
* Adjust the leaf header values.
|
|
|
|
*/
|
2013-04-11 21:30:21 +00:00
|
|
|
leafhdr->count -= from - to;
|
|
|
|
leafhdr->stale = 1;
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* Remember the low/high stale value only in the "right"
|
|
|
|
* direction.
|
|
|
|
*/
|
|
|
|
if (lowstale >= newindex)
|
|
|
|
lowstale = -1;
|
|
|
|
else
|
2013-04-11 21:30:21 +00:00
|
|
|
highstale = leafhdr->count;
|
|
|
|
*highlogp = leafhdr->count - 1;
|
2005-04-16 22:20:36 +00:00
|
|
|
*lowstalep = lowstale;
|
|
|
|
*highstalep = highstale;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Log the bests entries indicated from a leaf1 block.
|
|
|
|
*/
|
2005-06-21 05:36:52 +00:00
|
|
|
static void
|
2013-04-11 21:30:21 +00:00
|
|
|
xfs_dir3_leaf_log_bests(
|
2014-06-06 05:20:54 +00:00
|
|
|
struct xfs_da_args *args,
|
2012-06-22 08:50:14 +00:00
|
|
|
struct xfs_buf *bp, /* leaf buffer */
|
2005-04-16 22:20:36 +00:00
|
|
|
int first, /* first entry to log */
|
|
|
|
int last) /* last entry to log */
|
|
|
|
{
|
2006-03-17 06:27:19 +00:00
|
|
|
__be16 *firstb; /* pointer to first entry */
|
|
|
|
__be16 *lastb; /* pointer to last entry */
|
2013-04-11 21:30:21 +00:00
|
|
|
struct xfs_dir2_leaf *leaf = bp->b_addr;
|
2005-04-16 22:20:36 +00:00
|
|
|
xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
|
|
|
|
|
2013-04-11 21:30:21 +00:00
|
|
|
ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
|
|
|
|
leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC));
|
|
|
|
|
2014-06-06 05:20:54 +00:00
|
|
|
ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
|
2007-06-28 06:43:50 +00:00
|
|
|
firstb = xfs_dir2_leaf_bests_p(ltp) + first;
|
|
|
|
lastb = xfs_dir2_leaf_bests_p(ltp) + last;
|
2014-06-06 05:20:54 +00:00
|
|
|
xfs_trans_log_buf(args->trans, bp,
|
|
|
|
(uint)((char *)firstb - (char *)leaf),
|
2005-04-16 22:20:36 +00:00
|
|
|
(uint)((char *)lastb - (char *)leaf + sizeof(*lastb) - 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Log the leaf entries indicated from a leaf1 or leafn block.
|
|
|
|
*/
|
|
|
|
void
|
2013-04-11 21:30:21 +00:00
|
|
|
xfs_dir3_leaf_log_ents(
|
2014-06-06 05:20:54 +00:00
|
|
|
struct xfs_da_args *args,
|
2019-11-08 22:57:50 +00:00
|
|
|
struct xfs_dir3_icleaf_hdr *hdr,
|
2013-10-29 11:11:50 +00:00
|
|
|
struct xfs_buf *bp,
|
|
|
|
int first,
|
|
|
|
int last)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
xfs_dir2_leaf_entry_t *firstlep; /* pointer to first entry */
|
|
|
|
xfs_dir2_leaf_entry_t *lastlep; /* pointer to last entry */
|
2013-04-11 21:30:21 +00:00
|
|
|
struct xfs_dir2_leaf *leaf = bp->b_addr;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2011-07-08 12:36:05 +00:00
|
|
|
ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
|
2013-04-11 21:30:21 +00:00
|
|
|
leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
|
|
|
|
leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
|
|
|
|
leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC));
|
|
|
|
|
2019-11-08 22:57:50 +00:00
|
|
|
firstlep = &hdr->ents[first];
|
|
|
|
lastlep = &hdr->ents[last];
|
2014-06-06 05:20:54 +00:00
|
|
|
xfs_trans_log_buf(args->trans, bp,
|
|
|
|
(uint)((char *)firstlep - (char *)leaf),
|
2005-04-16 22:20:36 +00:00
|
|
|
(uint)((char *)lastlep - (char *)leaf + sizeof(*lastlep) - 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Log the header of the leaf1 or leafn block.
|
|
|
|
*/
|
|
|
|
void
|
2013-04-11 21:30:21 +00:00
|
|
|
xfs_dir3_leaf_log_header(
|
2014-06-06 05:20:54 +00:00
|
|
|
struct xfs_da_args *args,
|
2012-06-22 08:50:14 +00:00
|
|
|
struct xfs_buf *bp)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2013-04-11 21:30:21 +00:00
|
|
|
struct xfs_dir2_leaf *leaf = bp->b_addr;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2011-07-08 12:36:05 +00:00
|
|
|
ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
|
2013-04-11 21:30:21 +00:00
|
|
|
leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
|
|
|
|
leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
|
|
|
|
leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC));
|
|
|
|
|
2014-06-06 05:20:54 +00:00
|
|
|
xfs_trans_log_buf(args->trans, bp,
|
|
|
|
(uint)((char *)&leaf->hdr - (char *)leaf),
|
2019-11-08 22:57:51 +00:00
|
|
|
args->geo->leaf_hdr_size - 1);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Log the tail of the leaf1 block.
|
|
|
|
*/
|
2005-06-21 05:36:52 +00:00
|
|
|
STATIC void
|
2013-04-11 21:30:21 +00:00
|
|
|
xfs_dir3_leaf_log_tail(
|
2014-06-06 05:20:54 +00:00
|
|
|
struct xfs_da_args *args,
|
2012-06-22 08:50:14 +00:00
|
|
|
struct xfs_buf *bp)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2013-04-11 21:30:21 +00:00
|
|
|
struct xfs_dir2_leaf *leaf = bp->b_addr;
|
2005-04-16 22:20:36 +00:00
|
|
|
xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
|
2013-04-11 21:30:21 +00:00
|
|
|
|
|
|
|
ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
|
|
|
|
leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
|
|
|
|
leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
|
|
|
|
leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC));
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2014-06-06 05:20:54 +00:00
|
|
|
ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
|
|
|
|
xfs_trans_log_buf(args->trans, bp, (uint)((char *)ltp - (char *)leaf),
|
|
|
|
(uint)(args->geo->blksize - 1));
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Look up the entry referred to by args in the leaf format directory.
|
|
|
|
* Most of the work is done by the xfs_dir2_leaf_lookup_int routine which
|
|
|
|
* is also used by the node-format code.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
xfs_dir2_leaf_lookup(
|
|
|
|
xfs_da_args_t *args) /* operation arguments */
|
|
|
|
{
|
2012-06-22 08:50:14 +00:00
|
|
|
struct xfs_buf *dbp; /* data block buffer */
|
2005-04-16 22:20:36 +00:00
|
|
|
xfs_dir2_data_entry_t *dep; /* data block entry */
|
|
|
|
xfs_inode_t *dp; /* incore directory inode */
|
|
|
|
int error; /* error return code */
|
|
|
|
int index; /* found entry index */
|
2012-06-22 08:50:14 +00:00
|
|
|
struct xfs_buf *lbp; /* leaf buffer */
|
2005-04-16 22:20:36 +00:00
|
|
|
xfs_dir2_leaf_entry_t *lep; /* leaf entry */
|
|
|
|
xfs_trans_t *tp; /* transaction pointer */
|
2019-11-08 22:57:50 +00:00
|
|
|
struct xfs_dir3_icleaf_hdr leafhdr;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2009-12-14 23:14:59 +00:00
|
|
|
trace_xfs_dir2_leaf_lookup(args);
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* Look up name in the leaf block, returning both buffers and index.
|
|
|
|
*/
|
2019-11-08 22:57:50 +00:00
|
|
|
error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp, &leafhdr);
|
|
|
|
if (error)
|
2005-04-16 22:20:36 +00:00
|
|
|
return error;
|
2019-11-08 22:57:50 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
tp = args->trans;
|
|
|
|
dp = args->dp;
|
2013-10-29 11:11:50 +00:00
|
|
|
xfs_dir3_leaf_check(dp, lbp);
|
2019-11-08 22:57:50 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* Get to the leaf entry and contained data entry address.
|
|
|
|
*/
|
2019-11-08 22:57:50 +00:00
|
|
|
lep = &leafhdr.ents[index];
|
2013-04-11 21:30:21 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* Point to the data entry.
|
|
|
|
*/
|
|
|
|
dep = (xfs_dir2_data_entry_t *)
|
2012-06-22 08:50:14 +00:00
|
|
|
((char *)dbp->b_addr +
|
2014-06-06 05:08:18 +00:00
|
|
|
xfs_dir2_dataptr_to_off(args->geo, be32_to_cpu(lep->address)));
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
2008-05-21 06:58:22 +00:00
|
|
|
* Return the found inode number & CI name if appropriate
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
2006-06-09 04:48:37 +00:00
|
|
|
args->inumber = be64_to_cpu(dep->inumber);
|
2019-11-08 23:05:48 +00:00
|
|
|
args->filetype = xfs_dir2_data_get_ftype(dp->i_mount, dep);
|
2008-05-21 06:58:22 +00:00
|
|
|
error = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
|
2012-06-22 08:50:14 +00:00
|
|
|
xfs_trans_brelse(tp, dbp);
|
|
|
|
xfs_trans_brelse(tp, lbp);
|
2014-06-22 05:04:54 +00:00
|
|
|
return error;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Look up name/hash in the leaf block.
|
|
|
|
* Fill in indexp with the found index, and dbpp with the data buffer.
|
|
|
|
* If not found dbpp will be NULL, and ENOENT comes back.
|
|
|
|
* lbpp will always be filled in with the leaf buffer unless there's an error.
|
|
|
|
*/
|
|
|
|
static int /* error */
|
|
|
|
xfs_dir2_leaf_lookup_int(
|
|
|
|
xfs_da_args_t *args, /* operation arguments */
|
2012-06-22 08:50:14 +00:00
|
|
|
struct xfs_buf **lbpp, /* out: leaf buffer */
|
2005-04-16 22:20:36 +00:00
|
|
|
int *indexp, /* out: index in leaf block */
|
2019-11-08 22:57:50 +00:00
|
|
|
struct xfs_buf **dbpp, /* out: data buffer */
|
|
|
|
struct xfs_dir3_icleaf_hdr *leafhdr)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2008-06-27 03:32:11 +00:00
|
|
|
xfs_dir2_db_t curdb = -1; /* current data block number */
|
2012-06-22 08:50:14 +00:00
|
|
|
struct xfs_buf *dbp = NULL; /* data buffer */
|
2005-04-16 22:20:36 +00:00
|
|
|
xfs_dir2_data_entry_t *dep; /* data entry */
|
|
|
|
xfs_inode_t *dp; /* incore directory inode */
|
|
|
|
int error; /* error return code */
|
|
|
|
int index; /* index in leaf block */
|
2012-06-22 08:50:14 +00:00
|
|
|
struct xfs_buf *lbp; /* leaf buffer */
|
2005-04-16 22:20:36 +00:00
|
|
|
xfs_dir2_leaf_entry_t *lep; /* leaf entry */
|
|
|
|
xfs_dir2_leaf_t *leaf; /* leaf structure */
|
|
|
|
xfs_mount_t *mp; /* filesystem mount point */
|
|
|
|
xfs_dir2_db_t newdb; /* new data block number */
|
|
|
|
xfs_trans_t *tp; /* transaction pointer */
|
2008-06-27 03:32:11 +00:00
|
|
|
xfs_dir2_db_t cidb = -1; /* case match data block no. */
|
2008-05-21 06:41:01 +00:00
|
|
|
enum xfs_dacmp cmp; /* name compare result */
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
dp = args->dp;
|
|
|
|
tp = args->trans;
|
|
|
|
mp = dp->i_mount;
|
2012-11-12 11:54:15 +00:00
|
|
|
|
2024-04-15 21:54:39 +00:00
|
|
|
error = xfs_dir3_leaf_read(tp, dp, args->owner, args->geo->leafblk,
|
|
|
|
&lbp);
|
2008-06-27 03:32:11 +00:00
|
|
|
if (error)
|
2005-04-16 22:20:36 +00:00
|
|
|
return error;
|
2012-11-12 11:54:15 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
*lbpp = lbp;
|
2012-06-22 08:50:14 +00:00
|
|
|
leaf = lbp->b_addr;
|
2013-10-29 11:11:50 +00:00
|
|
|
xfs_dir3_leaf_check(dp, lbp);
|
2019-11-08 22:57:50 +00:00
|
|
|
xfs_dir2_leaf_hdr_from_disk(mp, leafhdr, leaf);
|
2013-04-11 21:30:21 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* Look for the first leaf entry with our hash value.
|
|
|
|
*/
|
|
|
|
index = xfs_dir2_leaf_search_hash(args, lbp);
|
|
|
|
/*
|
|
|
|
* Loop over all the entries with the right hash value
|
|
|
|
* looking to match the name.
|
|
|
|
*/
|
2019-11-08 22:57:50 +00:00
|
|
|
for (lep = &leafhdr->ents[index];
|
|
|
|
index < leafhdr->count &&
|
|
|
|
be32_to_cpu(lep->hashval) == args->hashval;
|
2013-04-11 21:30:21 +00:00
|
|
|
lep++, index++) {
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* Skip over stale leaf entries.
|
|
|
|
*/
|
2006-03-17 06:28:18 +00:00
|
|
|
if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
|
2005-04-16 22:20:36 +00:00
|
|
|
continue;
|
|
|
|
/*
|
|
|
|
* Get the new data block number.
|
|
|
|
*/
|
2014-06-06 05:08:18 +00:00
|
|
|
newdb = xfs_dir2_dataptr_to_db(args->geo,
|
|
|
|
be32_to_cpu(lep->address));
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* If it's not the same as the old data block number,
|
|
|
|
* need to pitch the old one and read the new one.
|
|
|
|
*/
|
|
|
|
if (newdb != curdb) {
|
2008-06-27 03:32:11 +00:00
|
|
|
if (dbp)
|
2012-06-22 08:50:14 +00:00
|
|
|
xfs_trans_brelse(tp, dbp);
|
2024-04-15 21:54:40 +00:00
|
|
|
error = xfs_dir3_data_read(tp, dp, args->owner,
|
|
|
|
xfs_dir2_db_to_da(args->geo, newdb), 0,
|
|
|
|
&dbp);
|
2008-05-21 06:41:01 +00:00
|
|
|
if (error) {
|
2012-06-22 08:50:14 +00:00
|
|
|
xfs_trans_brelse(tp, lbp);
|
2005-04-16 22:20:36 +00:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
curdb = newdb;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Point to the data entry.
|
|
|
|
*/
|
2012-06-22 08:50:14 +00:00
|
|
|
dep = (xfs_dir2_data_entry_t *)((char *)dbp->b_addr +
|
2014-06-06 05:08:18 +00:00
|
|
|
xfs_dir2_dataptr_to_off(args->geo,
|
|
|
|
be32_to_cpu(lep->address)));
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
2008-05-21 06:41:01 +00:00
|
|
|
* Compare name and if it's an exact match, return the index
|
|
|
|
* and buffer. If it's the first case-insensitive match, store
|
|
|
|
* the index and buffer and continue looking for an exact match.
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
2019-11-11 20:59:26 +00:00
|
|
|
cmp = xfs_dir2_compname(args, dep->name, dep->namelen);
|
2008-05-21 06:41:01 +00:00
|
|
|
if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
|
|
|
|
args->cmpresult = cmp;
|
2005-04-16 22:20:36 +00:00
|
|
|
*indexp = index;
|
2008-06-27 03:32:11 +00:00
|
|
|
/* case exact match: return the current buffer. */
|
2008-05-21 06:41:01 +00:00
|
|
|
if (cmp == XFS_CMP_EXACT) {
|
|
|
|
*dbpp = dbp;
|
|
|
|
return 0;
|
|
|
|
}
|
2008-06-27 03:32:11 +00:00
|
|
|
cidb = curdb;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
}
|
2008-05-21 06:42:05 +00:00
|
|
|
ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
|
2008-05-21 06:41:01 +00:00
|
|
|
/*
|
2008-06-27 03:32:11 +00:00
|
|
|
* Here, we can only be doing a lookup (not a rename or remove).
|
|
|
|
* If a case-insensitive match was found earlier, re-read the
|
|
|
|
* appropriate data block if required and return it.
|
2008-05-21 06:41:01 +00:00
|
|
|
*/
|
|
|
|
if (args->cmpresult == XFS_CMP_CASE) {
|
2008-06-27 03:32:11 +00:00
|
|
|
ASSERT(cidb != -1);
|
|
|
|
if (cidb != curdb) {
|
2012-06-22 08:50:14 +00:00
|
|
|
xfs_trans_brelse(tp, dbp);
|
2024-04-15 21:54:40 +00:00
|
|
|
error = xfs_dir3_data_read(tp, dp, args->owner,
|
|
|
|
xfs_dir2_db_to_da(args->geo, cidb), 0,
|
|
|
|
&dbp);
|
2008-06-27 03:32:11 +00:00
|
|
|
if (error) {
|
2012-06-22 08:50:14 +00:00
|
|
|
xfs_trans_brelse(tp, lbp);
|
2008-06-27 03:32:11 +00:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*dbpp = dbp;
|
2008-05-21 06:41:01 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
2014-06-25 04:58:08 +00:00
|
|
|
* No match found, return -ENOENT.
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
2008-06-27 03:32:11 +00:00
|
|
|
ASSERT(cidb == -1);
|
2005-04-16 22:20:36 +00:00
|
|
|
if (dbp)
|
2012-06-22 08:50:14 +00:00
|
|
|
xfs_trans_brelse(tp, dbp);
|
|
|
|
xfs_trans_brelse(tp, lbp);
|
2014-06-25 04:58:08 +00:00
|
|
|
return -ENOENT;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Remove an entry from a leaf format directory.
|
|
|
|
*/
|
|
|
|
int /* error */
|
|
|
|
xfs_dir2_leaf_removename(
|
|
|
|
xfs_da_args_t *args) /* operation arguments */
|
|
|
|
{
|
2019-11-08 23:05:38 +00:00
|
|
|
struct xfs_da_geometry *geo = args->geo;
|
2006-03-17 06:27:19 +00:00
|
|
|
__be16 *bestsp; /* leaf block best freespace */
|
2011-07-08 12:35:38 +00:00
|
|
|
xfs_dir2_data_hdr_t *hdr; /* data block header */
|
2005-04-16 22:20:36 +00:00
|
|
|
xfs_dir2_db_t db; /* data block number */
|
2012-06-22 08:50:14 +00:00
|
|
|
struct xfs_buf *dbp; /* data block buffer */
|
2005-04-16 22:20:36 +00:00
|
|
|
xfs_dir2_data_entry_t *dep; /* data entry structure */
|
|
|
|
xfs_inode_t *dp; /* incore directory inode */
|
|
|
|
int error; /* error return code */
|
|
|
|
xfs_dir2_db_t i; /* temporary data block # */
|
|
|
|
int index; /* index into leaf entries */
|
2012-06-22 08:50:14 +00:00
|
|
|
struct xfs_buf *lbp; /* leaf buffer */
|
2005-04-16 22:20:36 +00:00
|
|
|
xfs_dir2_leaf_t *leaf; /* leaf structure */
|
|
|
|
xfs_dir2_leaf_entry_t *lep; /* leaf entry */
|
|
|
|
xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
|
|
|
|
int needlog; /* need to log data header */
|
|
|
|
int needscan; /* need to rescan data frees */
|
|
|
|
xfs_dir2_data_off_t oldbest; /* old value of best free */
|
2013-04-03 05:11:22 +00:00
|
|
|
struct xfs_dir2_data_free *bf; /* bestfree table */
|
2013-04-11 21:30:21 +00:00
|
|
|
struct xfs_dir3_icleaf_hdr leafhdr;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2009-12-14 23:14:59 +00:00
|
|
|
trace_xfs_dir2_leaf_removename(args);
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* Lookup the leaf entry, get the leaf and data blocks read in.
|
|
|
|
*/
|
2019-11-08 22:57:50 +00:00
|
|
|
error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp, &leafhdr);
|
|
|
|
if (error)
|
2005-04-16 22:20:36 +00:00
|
|
|
return error;
|
2019-11-08 22:57:50 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
dp = args->dp;
|
2012-06-22 08:50:14 +00:00
|
|
|
leaf = lbp->b_addr;
|
|
|
|
hdr = dbp->b_addr;
|
2013-04-03 05:11:22 +00:00
|
|
|
xfs_dir3_data_check(dp, dbp);
|
2019-11-08 23:05:39 +00:00
|
|
|
bf = xfs_dir2_data_bestfree_p(dp->i_mount, hdr);
|
2019-11-08 22:57:50 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* Point to the leaf entry, use that to point to the data entry.
|
|
|
|
*/
|
2019-11-08 22:57:50 +00:00
|
|
|
lep = &leafhdr.ents[index];
|
2019-11-08 23:05:38 +00:00
|
|
|
db = xfs_dir2_dataptr_to_db(geo, be32_to_cpu(lep->address));
|
2014-06-06 05:08:18 +00:00
|
|
|
dep = (xfs_dir2_data_entry_t *)((char *)hdr +
|
2019-11-08 23:05:38 +00:00
|
|
|
xfs_dir2_dataptr_to_off(geo, be32_to_cpu(lep->address)));
|
2005-04-16 22:20:36 +00:00
|
|
|
needscan = needlog = 0;
|
2013-04-03 05:11:22 +00:00
|
|
|
oldbest = be16_to_cpu(bf[0].length);
|
2019-11-08 23:05:38 +00:00
|
|
|
ltp = xfs_dir2_leaf_tail_p(geo, leaf);
|
2007-06-28 06:43:50 +00:00
|
|
|
bestsp = xfs_dir2_leaf_bests_p(ltp);
|
2019-11-02 16:40:53 +00:00
|
|
|
if (be16_to_cpu(bestsp[db]) != oldbest) {
|
2020-03-11 17:37:54 +00:00
|
|
|
xfs_buf_mark_corrupt(lbp);
|
2024-02-22 20:32:18 +00:00
|
|
|
xfs_da_mark_sick(args);
|
2018-03-07 01:08:31 +00:00
|
|
|
return -EFSCORRUPTED;
|
2019-11-02 16:40:53 +00:00
|
|
|
}
|
2024-02-22 20:32:18 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* Mark the former data entry unused.
|
|
|
|
*/
|
2014-06-06 05:20:54 +00:00
|
|
|
xfs_dir2_data_make_free(args, dbp,
|
2011-07-08 12:35:38 +00:00
|
|
|
(xfs_dir2_data_aoff_t)((char *)dep - (char *)hdr),
|
2019-11-08 23:05:37 +00:00
|
|
|
xfs_dir2_data_entsize(dp->i_mount, dep->namelen), &needlog,
|
|
|
|
&needscan);
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* We just mark the leaf entry stale by putting a null in it.
|
|
|
|
*/
|
2013-04-11 21:30:21 +00:00
|
|
|
leafhdr.stale++;
|
2019-11-08 22:57:50 +00:00
|
|
|
xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf, &leafhdr);
|
2014-06-06 05:20:54 +00:00
|
|
|
xfs_dir3_leaf_log_header(args, lbp);
|
2013-04-11 21:30:21 +00:00
|
|
|
|
2006-03-17 06:28:18 +00:00
|
|
|
lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
|
2019-11-08 22:57:50 +00:00
|
|
|
xfs_dir3_leaf_log_ents(args, &leafhdr, lbp, index, index);
|
2013-04-11 21:30:21 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* Scan the freespace in the data block again if necessary,
|
|
|
|
* log the data block header if necessary.
|
|
|
|
*/
|
|
|
|
if (needscan)
|
2019-11-08 23:06:02 +00:00
|
|
|
xfs_dir2_data_freescan(dp->i_mount, hdr, &needlog);
|
2005-04-16 22:20:36 +00:00
|
|
|
if (needlog)
|
2014-06-06 05:20:54 +00:00
|
|
|
xfs_dir2_data_log_header(args, dbp);
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* If the longest freespace in the data block has changed,
|
|
|
|
* put the new value in the bests table and log that.
|
|
|
|
*/
|
2013-04-03 05:11:22 +00:00
|
|
|
if (be16_to_cpu(bf[0].length) != oldbest) {
|
|
|
|
bestsp[db] = bf[0].length;
|
2014-06-06 05:20:54 +00:00
|
|
|
xfs_dir3_leaf_log_bests(args, lbp, db, db);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2013-04-03 05:11:22 +00:00
|
|
|
xfs_dir3_data_check(dp, dbp);
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* If the data block is now empty then get rid of the data block.
|
|
|
|
*/
|
2013-04-03 05:11:22 +00:00
|
|
|
if (be16_to_cpu(bf[0].length) ==
|
2019-11-08 23:05:38 +00:00
|
|
|
geo->blksize - geo->data_entry_offset) {
|
|
|
|
ASSERT(db != geo->datablk);
|
2005-04-16 22:20:36 +00:00
|
|
|
if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
|
|
|
|
/*
|
|
|
|
* Nope, can't get rid of it because it caused
|
|
|
|
* allocation of a bmap btree block to do so.
|
|
|
|
* Just go on, returning success, leaving the
|
|
|
|
* empty block in place.
|
|
|
|
*/
|
2014-06-25 04:58:08 +00:00
|
|
|
if (error == -ENOSPC && args->total == 0)
|
2005-04-16 22:20:36 +00:00
|
|
|
error = 0;
|
2013-10-29 11:11:50 +00:00
|
|
|
xfs_dir3_leaf_check(dp, lbp);
|
2005-04-16 22:20:36 +00:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
dbp = NULL;
|
|
|
|
/*
|
|
|
|
* If this is the last data block then compact the
|
|
|
|
* bests table by getting rid of entries.
|
|
|
|
*/
|
2006-03-17 06:27:28 +00:00
|
|
|
if (db == be32_to_cpu(ltp->bestcount) - 1) {
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* Look for the last active entry (i).
|
|
|
|
*/
|
|
|
|
for (i = db - 1; i > 0; i--) {
|
2011-07-08 12:36:05 +00:00
|
|
|
if (bestsp[i] != cpu_to_be16(NULLDATAOFF))
|
2005-04-16 22:20:36 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Copy the table down so inactive entries at the
|
|
|
|
* end are removed.
|
|
|
|
*/
|
|
|
|
memmove(&bestsp[db - i], bestsp,
|
2006-03-17 06:27:28 +00:00
|
|
|
(be32_to_cpu(ltp->bestcount) - (db - i)) * sizeof(*bestsp));
|
2008-02-13 23:03:29 +00:00
|
|
|
be32_add_cpu(<p->bestcount, -(db - i));
|
2014-06-06 05:20:54 +00:00
|
|
|
xfs_dir3_leaf_log_tail(args, lbp);
|
|
|
|
xfs_dir3_leaf_log_bests(args, lbp, 0,
|
|
|
|
be32_to_cpu(ltp->bestcount) - 1);
|
2005-04-16 22:20:36 +00:00
|
|
|
} else
|
2006-03-17 06:27:19 +00:00
|
|
|
bestsp[db] = cpu_to_be16(NULLDATAOFF);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* If the data block was not the first one, drop it.
|
|
|
|
*/
|
2019-11-08 23:05:38 +00:00
|
|
|
else if (db != geo->datablk)
|
2005-04-16 22:20:36 +00:00
|
|
|
dbp = NULL;
|
2012-06-22 08:50:14 +00:00
|
|
|
|
2013-10-29 11:11:50 +00:00
|
|
|
xfs_dir3_leaf_check(dp, lbp);
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* See if we can convert to block form.
|
|
|
|
*/
|
|
|
|
return xfs_dir2_leaf_to_block(args, lbp, dbp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Replace the inode number in a leaf format directory entry.
|
|
|
|
*/
|
|
|
|
int /* error */
|
|
|
|
xfs_dir2_leaf_replace(
|
|
|
|
xfs_da_args_t *args) /* operation arguments */
|
|
|
|
{
|
2012-06-22 08:50:14 +00:00
|
|
|
struct xfs_buf *dbp; /* data block buffer */
|
2005-04-16 22:20:36 +00:00
|
|
|
xfs_dir2_data_entry_t *dep; /* data block entry */
|
|
|
|
xfs_inode_t *dp; /* incore directory inode */
|
|
|
|
int error; /* error return code */
|
|
|
|
int index; /* index of leaf entry */
|
2012-06-22 08:50:14 +00:00
|
|
|
struct xfs_buf *lbp; /* leaf buffer */
|
2005-04-16 22:20:36 +00:00
|
|
|
xfs_dir2_leaf_entry_t *lep; /* leaf entry */
|
|
|
|
xfs_trans_t *tp; /* transaction pointer */
|
2019-11-08 22:57:50 +00:00
|
|
|
struct xfs_dir3_icleaf_hdr leafhdr;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2009-12-14 23:14:59 +00:00
|
|
|
trace_xfs_dir2_leaf_replace(args);
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* Look up the entry.
|
|
|
|
*/
|
2019-11-08 22:57:50 +00:00
|
|
|
error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp, &leafhdr);
|
|
|
|
if (error)
|
2005-04-16 22:20:36 +00:00
|
|
|
return error;
|
2019-11-08 22:57:50 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
dp = args->dp;
|
|
|
|
/*
|
|
|
|
* Point to the leaf entry, get data address from it.
|
|
|
|
*/
|
2019-11-08 22:57:50 +00:00
|
|
|
lep = &leafhdr.ents[index];
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* Point to the data entry.
|
|
|
|
*/
|
|
|
|
dep = (xfs_dir2_data_entry_t *)
|
2012-06-22 08:50:14 +00:00
|
|
|
((char *)dbp->b_addr +
|
2014-06-06 05:08:18 +00:00
|
|
|
xfs_dir2_dataptr_to_off(args->geo, be32_to_cpu(lep->address)));
|
2006-06-09 04:48:37 +00:00
|
|
|
ASSERT(args->inumber != be64_to_cpu(dep->inumber));
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* Put the new inode number in, log it.
|
|
|
|
*/
|
2006-06-09 04:48:37 +00:00
|
|
|
dep->inumber = cpu_to_be64(args->inumber);
|
2019-11-08 23:05:48 +00:00
|
|
|
xfs_dir2_data_put_ftype(dp->i_mount, dep, args->filetype);
|
2005-04-16 22:20:36 +00:00
|
|
|
tp = args->trans;
|
2014-06-06 05:20:54 +00:00
|
|
|
xfs_dir2_data_log_entry(args, dbp, dep);
|
2013-10-29 11:11:50 +00:00
|
|
|
xfs_dir3_leaf_check(dp, lbp);
|
2012-06-22 08:50:14 +00:00
|
|
|
xfs_trans_brelse(tp, lbp);
|
2005-04-16 22:20:36 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return index in the leaf block (lbp) which is either the first
|
|
|
|
* one with this hash value, or if there are none, the insert point
|
|
|
|
* for that hash value.
|
|
|
|
*/
|
|
|
|
int /* index value */
|
|
|
|
xfs_dir2_leaf_search_hash(
|
|
|
|
xfs_da_args_t *args, /* operation arguments */
|
2012-06-22 08:50:14 +00:00
|
|
|
struct xfs_buf *lbp) /* leaf buffer */
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
xfs_dahash_t hash=0; /* hash from this entry */
|
|
|
|
xfs_dahash_t hashwant; /* hash value looking for */
|
|
|
|
int high; /* high leaf index */
|
|
|
|
int low; /* low leaf index */
|
|
|
|
xfs_dir2_leaf_entry_t *lep; /* leaf entry */
|
|
|
|
int mid=0; /* current leaf index */
|
2013-04-11 21:30:21 +00:00
|
|
|
struct xfs_dir3_icleaf_hdr leafhdr;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2019-11-08 22:57:50 +00:00
|
|
|
xfs_dir2_leaf_hdr_from_disk(args->dp->i_mount, &leafhdr, lbp->b_addr);
|
2013-04-11 21:30:21 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* Note, the table cannot be empty, so we have to go through the loop.
|
|
|
|
* Binary search the leaf entries looking for our hash value.
|
|
|
|
*/
|
2019-11-08 22:57:50 +00:00
|
|
|
for (lep = leafhdr.ents, low = 0, high = leafhdr.count - 1,
|
2005-04-16 22:20:36 +00:00
|
|
|
hashwant = args->hashval;
|
|
|
|
low <= high; ) {
|
|
|
|
mid = (low + high) >> 1;
|
2006-03-17 06:28:18 +00:00
|
|
|
if ((hash = be32_to_cpu(lep[mid].hashval)) == hashwant)
|
2005-04-16 22:20:36 +00:00
|
|
|
break;
|
|
|
|
if (hash < hashwant)
|
|
|
|
low = mid + 1;
|
|
|
|
else
|
|
|
|
high = mid - 1;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Found one, back up through all the equal hash values.
|
|
|
|
*/
|
|
|
|
if (hash == hashwant) {
|
2006-03-17 06:28:18 +00:00
|
|
|
while (mid > 0 && be32_to_cpu(lep[mid - 1].hashval) == hashwant) {
|
2005-04-16 22:20:36 +00:00
|
|
|
mid--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Need to point to an entry higher than ours.
|
|
|
|
*/
|
|
|
|
else if (hash < hashwant)
|
|
|
|
mid++;
|
|
|
|
return mid;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Trim off a trailing data block. We know it's empty since the leaf
|
|
|
|
* freespace table says so.
|
|
|
|
*/
|
|
|
|
int /* error */
|
|
|
|
xfs_dir2_leaf_trim_data(
|
|
|
|
xfs_da_args_t *args, /* operation arguments */
|
2012-06-22 08:50:14 +00:00
|
|
|
struct xfs_buf *lbp, /* leaf buffer */
|
2005-04-16 22:20:36 +00:00
|
|
|
xfs_dir2_db_t db) /* data block number */
|
|
|
|
{
|
2019-11-08 23:05:38 +00:00
|
|
|
struct xfs_da_geometry *geo = args->geo;
|
2006-03-17 06:27:19 +00:00
|
|
|
__be16 *bestsp; /* leaf bests table */
|
2012-06-22 08:50:14 +00:00
|
|
|
struct xfs_buf *dbp; /* data block buffer */
|
2005-04-16 22:20:36 +00:00
|
|
|
xfs_inode_t *dp; /* incore directory inode */
|
|
|
|
int error; /* error return value */
|
|
|
|
xfs_dir2_leaf_t *leaf; /* leaf structure */
|
|
|
|
xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
|
|
|
|
xfs_trans_t *tp; /* transaction pointer */
|
|
|
|
|
|
|
|
dp = args->dp;
|
|
|
|
tp = args->trans;
|
|
|
|
/*
|
|
|
|
* Read the offending data block. We need its buffer.
|
|
|
|
*/
|
2024-04-15 21:54:40 +00:00
|
|
|
error = xfs_dir3_data_read(tp, dp, args->owner,
|
|
|
|
xfs_dir2_db_to_da(geo, db), 0, &dbp);
|
2012-11-12 11:54:10 +00:00
|
|
|
if (error)
|
2005-04-16 22:20:36 +00:00
|
|
|
return error;
|
|
|
|
|
2012-06-22 08:50:14 +00:00
|
|
|
leaf = lbp->b_addr;
|
2019-11-08 23:05:38 +00:00
|
|
|
ltp = xfs_dir2_leaf_tail_p(geo, leaf);
|
2011-07-08 12:35:38 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
{
|
2012-06-22 08:50:14 +00:00
|
|
|
struct xfs_dir2_data_hdr *hdr = dbp->b_addr;
|
2019-11-08 23:05:39 +00:00
|
|
|
struct xfs_dir2_data_free *bf =
|
|
|
|
xfs_dir2_data_bestfree_p(dp->i_mount, hdr);
|
2011-07-08 12:35:38 +00:00
|
|
|
|
2013-04-03 05:11:22 +00:00
|
|
|
ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
|
|
|
|
hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC));
|
|
|
|
ASSERT(be16_to_cpu(bf[0].length) ==
|
2019-11-08 23:05:38 +00:00
|
|
|
geo->blksize - geo->data_entry_offset);
|
2006-03-17 06:27:28 +00:00
|
|
|
ASSERT(db == be32_to_cpu(ltp->bestcount) - 1);
|
2011-07-08 12:35:38 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* Get rid of the data block.
|
|
|
|
*/
|
|
|
|
if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
|
2014-06-25 04:58:08 +00:00
|
|
|
ASSERT(error != -ENOSPC);
|
2012-06-22 08:50:14 +00:00
|
|
|
xfs_trans_brelse(tp, dbp);
|
2005-04-16 22:20:36 +00:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Eliminate the last bests entry from the table.
|
|
|
|
*/
|
2007-06-28 06:43:50 +00:00
|
|
|
bestsp = xfs_dir2_leaf_bests_p(ltp);
|
2008-02-13 23:03:29 +00:00
|
|
|
be32_add_cpu(<p->bestcount, -1);
|
2006-03-17 06:27:28 +00:00
|
|
|
memmove(&bestsp[1], &bestsp[0], be32_to_cpu(ltp->bestcount) * sizeof(*bestsp));
|
2014-06-06 05:20:54 +00:00
|
|
|
xfs_dir3_leaf_log_tail(args, lbp);
|
|
|
|
xfs_dir3_leaf_log_bests(args, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
|
2005-04-16 22:20:36 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-07-08 12:35:53 +00:00
|
|
|
static inline size_t
|
2013-04-11 21:30:21 +00:00
|
|
|
xfs_dir3_leaf_size(
|
|
|
|
struct xfs_dir3_icleaf_hdr *hdr,
|
2011-07-08 12:35:53 +00:00
|
|
|
int counts)
|
|
|
|
{
|
2013-04-11 21:30:21 +00:00
|
|
|
int entries;
|
|
|
|
int hdrsize;
|
|
|
|
|
|
|
|
entries = hdr->count - hdr->stale;
|
|
|
|
if (hdr->magic == XFS_DIR2_LEAF1_MAGIC ||
|
|
|
|
hdr->magic == XFS_DIR2_LEAFN_MAGIC)
|
|
|
|
hdrsize = sizeof(struct xfs_dir2_leaf_hdr);
|
|
|
|
else
|
|
|
|
hdrsize = sizeof(struct xfs_dir3_leaf_hdr);
|
2011-07-08 12:35:53 +00:00
|
|
|
|
2013-04-11 21:30:21 +00:00
|
|
|
return hdrsize + entries * sizeof(xfs_dir2_leaf_entry_t)
|
|
|
|
+ counts * sizeof(xfs_dir2_data_off_t)
|
|
|
|
+ sizeof(xfs_dir2_leaf_tail_t);
|
2011-07-08 12:35:53 +00:00
|
|
|
}
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* Convert node form directory to leaf form directory.
|
|
|
|
* The root of the node form dir needs to already be a LEAFN block.
|
|
|
|
* Just return if we can't do anything.
|
|
|
|
*/
|
|
|
|
int /* error */
|
|
|
|
xfs_dir2_node_to_leaf(
|
|
|
|
xfs_da_state_t *state) /* directory operation state */
|
|
|
|
{
|
|
|
|
xfs_da_args_t *args; /* operation arguments */
|
|
|
|
xfs_inode_t *dp; /* incore directory inode */
|
|
|
|
int error; /* error return code */
|
2012-06-22 08:50:14 +00:00
|
|
|
struct xfs_buf *fbp; /* buffer for freespace block */
|
2005-04-16 22:20:36 +00:00
|
|
|
xfs_fileoff_t fo; /* freespace file offset */
|
2012-06-22 08:50:14 +00:00
|
|
|
struct xfs_buf *lbp; /* buffer for leaf block */
|
2005-04-16 22:20:36 +00:00
|
|
|
xfs_dir2_leaf_tail_t *ltp; /* tail of leaf structure */
|
|
|
|
xfs_dir2_leaf_t *leaf; /* leaf structure */
|
|
|
|
xfs_mount_t *mp; /* filesystem mount point */
|
|
|
|
int rval; /* successful free trim? */
|
|
|
|
xfs_trans_t *tp; /* transaction pointer */
|
2013-04-11 21:30:21 +00:00
|
|
|
struct xfs_dir3_icleaf_hdr leafhdr;
|
2013-04-03 05:11:21 +00:00
|
|
|
struct xfs_dir3_icfree_hdr freehdr;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* There's more than a leaf level in the btree, so there must
|
|
|
|
* be multiple leafn blocks. Give up.
|
|
|
|
*/
|
|
|
|
if (state->path.active > 1)
|
|
|
|
return 0;
|
|
|
|
args = state->args;
|
2009-12-14 23:14:59 +00:00
|
|
|
|
|
|
|
trace_xfs_dir2_node_to_leaf(args);
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
mp = state->mp;
|
|
|
|
dp = args->dp;
|
|
|
|
tp = args->trans;
|
|
|
|
/*
|
|
|
|
* Get the last offset in the file.
|
|
|
|
*/
|
2014-04-14 08:58:05 +00:00
|
|
|
if ((error = xfs_bmap_last_offset(dp, &fo, XFS_DATA_FORK))) {
|
2005-04-16 22:20:36 +00:00
|
|
|
return error;
|
|
|
|
}
|
2014-06-06 05:14:11 +00:00
|
|
|
fo -= args->geo->fsbcount;
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* If there are freespace blocks other than the first one,
|
|
|
|
* take this opportunity to remove trailing empty freespace blocks
|
|
|
|
* that may have been left behind during no-space-reservation
|
|
|
|
* operations.
|
|
|
|
*/
|
2014-06-06 05:11:18 +00:00
|
|
|
while (fo > args->geo->freeblk) {
|
2005-04-16 22:20:36 +00:00
|
|
|
if ((error = xfs_dir2_node_trim_free(args, fo, &rval))) {
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
if (rval)
|
2014-06-06 05:14:11 +00:00
|
|
|
fo -= args->geo->fsbcount;
|
2005-04-16 22:20:36 +00:00
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Now find the block just before the freespace block.
|
|
|
|
*/
|
|
|
|
if ((error = xfs_bmap_last_before(tp, dp, &fo, XFS_DATA_FORK))) {
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* If it's not the single leaf block, give up.
|
|
|
|
*/
|
2014-06-06 05:15:59 +00:00
|
|
|
if (XFS_FSB_TO_B(mp, fo) > XFS_DIR2_LEAF_OFFSET + args->geo->blksize)
|
2005-04-16 22:20:36 +00:00
|
|
|
return 0;
|
|
|
|
lbp = state->path.blk[0].bp;
|
2012-06-22 08:50:14 +00:00
|
|
|
leaf = lbp->b_addr;
|
2019-11-08 22:57:49 +00:00
|
|
|
xfs_dir2_leaf_hdr_from_disk(mp, &leafhdr, leaf);
|
2013-04-11 21:30:21 +00:00
|
|
|
|
|
|
|
ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC ||
|
|
|
|
leafhdr.magic == XFS_DIR3_LEAFN_MAGIC);
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* Read the freespace block.
|
|
|
|
*/
|
2024-04-15 21:54:41 +00:00
|
|
|
error = xfs_dir2_free_read(tp, dp, args->owner, args->geo->freeblk,
|
|
|
|
&fbp);
|
2012-11-12 11:54:10 +00:00
|
|
|
if (error)
|
2005-04-16 22:20:36 +00:00
|
|
|
return error;
|
2019-11-08 22:58:05 +00:00
|
|
|
xfs_dir2_free_hdr_from_disk(mp, &freehdr, fbp->b_addr);
|
2013-04-03 05:11:21 +00:00
|
|
|
|
|
|
|
ASSERT(!freehdr.firstdb);
|
2011-07-08 12:35:53 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* Now see if the leafn and free data will fit in a leaf1.
|
|
|
|
* If not, release the buffer and give up.
|
|
|
|
*/
|
2014-06-06 05:15:59 +00:00
|
|
|
if (xfs_dir3_leaf_size(&leafhdr, freehdr.nvalid) > args->geo->blksize) {
|
2012-06-22 08:50:14 +00:00
|
|
|
xfs_trans_brelse(tp, fbp);
|
2005-04-16 22:20:36 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2011-07-08 12:35:53 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* If the leaf has any stale entries in it, compress them out.
|
|
|
|
*/
|
2013-04-11 21:30:21 +00:00
|
|
|
if (leafhdr.stale)
|
|
|
|
xfs_dir3_leaf_compact(args, &leafhdr, lbp);
|
2012-11-14 06:53:49 +00:00
|
|
|
|
2013-04-11 21:30:21 +00:00
|
|
|
lbp->b_ops = &xfs_dir3_leaf1_buf_ops;
|
2013-04-03 05:11:30 +00:00
|
|
|
xfs_trans_buf_set_type(tp, lbp, XFS_BLFT_DIR_LEAF1_BUF);
|
2013-04-11 21:30:21 +00:00
|
|
|
leafhdr.magic = (leafhdr.magic == XFS_DIR2_LEAFN_MAGIC)
|
|
|
|
? XFS_DIR2_LEAF1_MAGIC
|
|
|
|
: XFS_DIR3_LEAF1_MAGIC;
|
2012-11-14 06:53:49 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* Set up the leaf tail from the freespace block.
|
|
|
|
*/
|
2014-06-06 05:15:59 +00:00
|
|
|
ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
|
2013-04-03 05:11:21 +00:00
|
|
|
ltp->bestcount = cpu_to_be32(freehdr.nvalid);
|
2013-04-11 21:30:21 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* Set up the leaf bests table.
|
|
|
|
*/
|
2019-11-08 22:58:05 +00:00
|
|
|
memcpy(xfs_dir2_leaf_bests_p(ltp), freehdr.bests,
|
2013-04-03 05:11:21 +00:00
|
|
|
freehdr.nvalid * sizeof(xfs_dir2_data_off_t));
|
2013-04-11 21:30:21 +00:00
|
|
|
|
2019-11-08 22:57:50 +00:00
|
|
|
xfs_dir2_leaf_hdr_to_disk(mp, leaf, &leafhdr);
|
2014-06-06 05:20:54 +00:00
|
|
|
xfs_dir3_leaf_log_header(args, lbp);
|
|
|
|
xfs_dir3_leaf_log_bests(args, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
|
|
|
|
xfs_dir3_leaf_log_tail(args, lbp);
|
2013-10-29 11:11:50 +00:00
|
|
|
xfs_dir3_leaf_check(dp, lbp);
|
2013-04-11 21:30:21 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* Get rid of the freespace block.
|
|
|
|
*/
|
2014-06-06 05:04:41 +00:00
|
|
|
error = xfs_dir2_shrink_inode(args,
|
2014-06-06 05:08:18 +00:00
|
|
|
xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET),
|
|
|
|
fbp);
|
2005-04-16 22:20:36 +00:00
|
|
|
if (error) {
|
|
|
|
/*
|
|
|
|
* This can't fail here because it can only happen when
|
|
|
|
* punching out the middle of an extent, and this is an
|
|
|
|
* isolated block.
|
|
|
|
*/
|
2014-06-25 04:58:08 +00:00
|
|
|
ASSERT(error != -ENOSPC);
|
2005-04-16 22:20:36 +00:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
fbp = NULL;
|
|
|
|
/*
|
|
|
|
* Now see if we can convert the single-leaf directory
|
|
|
|
* down to a block form directory.
|
|
|
|
* This routine always kills the dabuf for the leaf, so
|
|
|
|
* eliminate it from the path.
|
|
|
|
*/
|
|
|
|
error = xfs_dir2_leaf_to_block(args, lbp, NULL);
|
|
|
|
state->path.blk[0].bp = NULL;
|
|
|
|
return error;
|
|
|
|
}
|