1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (c) 2023-2024 Oracle. All Rights Reserved.
* Author: Darrick J. Wong <djwong@kernel.org>
*/
#include "xfs.h"
#include "xfs_fs.h"
#include "xfs_shared.h"
#include "xfs_format.h"
#include "xfs_trans_resv.h"
#include "xfs_mount.h"
#include "xfs_log_format.h"
#include "xfs_trans.h"
#include "xfs_inode.h"
#include "xfs_metafile.h"
#include "xfs_quota.h"
#include "xfs_qm.h"
#include "xfs_dir2.h"
#include "scrub/scrub.h"
#include "scrub/common.h"
#include "scrub/trace.h"
#include "scrub/readdir.h"
/*
* Metadata Directory Tree Paths
* =============================
*
* A filesystem with metadir enabled expects to find metadata structures
* attached to files that are accessible by walking a path down the metadata
* directory tree. Given the metadir path and the incore inode storing the
* metadata, this scrubber ensures that the ondisk metadir path points to the
* ondisk inode represented by the incore inode.
*/
struct xchk_metapath {
struct xfs_scrub *sc;
/* Name for lookup */
struct xfs_name xname;
/* Path for this metadata file and the parent directory */
const char *path;
const char *parent_path;
/* Directory parent of the metadata file. */
struct xfs_inode *dp;
/* Locks held on dp */
unsigned int dp_ilock_flags;
};
/* Release resources tracked in the buffer. */
static inline void
xchk_metapath_cleanup(
void *buf)
{
struct xchk_metapath *mpath = buf;
if (mpath->dp_ilock_flags)
xfs_iunlock(mpath->dp, mpath->dp_ilock_flags);
kfree(mpath->path);
}
int
xchk_setup_metapath(
struct xfs_scrub *sc)
{
if (!xfs_has_metadir(sc->mp))
return -ENOENT;
if (sc->sm->sm_gen)
return -EINVAL;
switch (sc->sm->sm_ino) {
case XFS_SCRUB_METAPATH_PROBE:
/* Just probing, nothing else to do. */
if (sc->sm->sm_agno)
return -EINVAL;
return 0;
default:
return -ENOENT;
}
}
/*
* Take the ILOCK on the metadata directory parent and child. We do not know
* that the metadata directory is not corrupt, so we lock the parent and try
* to lock the child. Returns 0 if successful, or -EINTR to abort the scrub.
*/
STATIC int
xchk_metapath_ilock_both(
struct xchk_metapath *mpath)
{
struct xfs_scrub *sc = mpath->sc;
int error = 0;
while (true) {
xfs_ilock(mpath->dp, XFS_ILOCK_EXCL);
if (xchk_ilock_nowait(sc, XFS_ILOCK_EXCL)) {
mpath->dp_ilock_flags |= XFS_ILOCK_EXCL;
return 0;
}
xfs_iunlock(mpath->dp, XFS_ILOCK_EXCL);
if (xchk_should_terminate(sc, &error))
return error;
delay(1);
}
ASSERT(0);
return -EINTR;
}
/* Unlock parent and child inodes. */
static inline void
xchk_metapath_iunlock(
struct xchk_metapath *mpath)
{
struct xfs_scrub *sc = mpath->sc;
xchk_iunlock(sc, XFS_ILOCK_EXCL);
mpath->dp_ilock_flags &= ~XFS_ILOCK_EXCL;
xfs_iunlock(mpath->dp, XFS_ILOCK_EXCL);
}
int
xchk_metapath(
struct xfs_scrub *sc)
{
struct xchk_metapath *mpath = sc->buf;
xfs_ino_t ino = NULLFSINO;
int error;
/* Just probing, nothing else to do. */
if (sc->sm->sm_ino == XFS_SCRUB_METAPATH_PROBE)
return 0;
/* Parent required to do anything else. */
if (mpath->dp == NULL) {
xchk_ino_set_corrupt(sc, sc->ip->i_ino);
return 0;
}
error = xchk_trans_alloc_empty(sc);
if (error)
return error;
error = xchk_metapath_ilock_both(mpath);
if (error)
goto out_cancel;
/* Make sure the parent dir has a dirent pointing to this file. */
error = xchk_dir_lookup(sc, mpath->dp, &mpath->xname, &ino);
trace_xchk_metapath_lookup(sc, mpath->path, mpath->dp, ino);
if (error == -ENOENT) {
/* No directory entry at all */
xchk_ino_set_corrupt(sc, sc->ip->i_ino);
error = 0;
goto out_ilock;
}
if (!xchk_fblock_xref_process_error(sc, XFS_DATA_FORK, 0, &error))
goto out_ilock;
if (ino != sc->ip->i_ino) {
/* Pointing to wrong inode */
xchk_ino_set_corrupt(sc, sc->ip->i_ino);
}
out_ilock:
xchk_metapath_iunlock(mpath);
out_cancel:
xchk_trans_cancel(sc);
return error;
}
|