1
2
3
4
5
6#include "xfs.h"
7#include "xfs_fs.h"
8#include "xfs_shared.h"
9#include "xfs_format.h"
10#include "xfs_trans_resv.h"
11#include "xfs_mount.h"
12#include "xfs_log_format.h"
13#include "xfs_inode.h"
14#include "xfs_icache.h"
15#include "xfs_dir2.h"
16#include "xfs_dir2_priv.h"
17#include "scrub/scrub.h"
18#include "scrub/common.h"
19
20
21int
22xchk_setup_parent(
23 struct xfs_scrub *sc)
24{
25 return xchk_setup_inode_contents(sc, 0);
26}
27
28
29
30
31
32struct xchk_parent_ctx {
33 struct dir_context dc;
34 struct xfs_scrub *sc;
35 xfs_ino_t ino;
36 xfs_nlink_t nlink;
37 bool cancelled;
38};
39
40
41STATIC int
42xchk_parent_actor(
43 struct dir_context *dc,
44 const char *name,
45 int namelen,
46 loff_t pos,
47 u64 ino,
48 unsigned type)
49{
50 struct xchk_parent_ctx *spc;
51 int error = 0;
52
53 spc = container_of(dc, struct xchk_parent_ctx, dc);
54 if (spc->ino == ino)
55 spc->nlink++;
56
57
58
59
60
61
62 if (xchk_should_terminate(spc->sc, &error))
63 spc->cancelled = true;
64
65 return error;
66}
67
68
69STATIC int
70xchk_parent_count_parent_dentries(
71 struct xfs_scrub *sc,
72 struct xfs_inode *parent,
73 xfs_nlink_t *nlink)
74{
75 struct xchk_parent_ctx spc = {
76 .dc.actor = xchk_parent_actor,
77 .ino = sc->ip->i_ino,
78 .sc = sc,
79 };
80 size_t bufsize;
81 loff_t oldpos;
82 uint lock_mode;
83 int error = 0;
84
85
86
87
88
89
90
91 lock_mode = xfs_ilock_data_map_shared(parent);
92 if (parent->i_df.if_nextents > 0)
93 error = xfs_dir3_data_readahead(parent, 0, 0);
94 xfs_iunlock(parent, lock_mode);
95 if (error)
96 return error;
97
98
99
100
101
102
103 bufsize = (size_t)min_t(loff_t, XFS_READDIR_BUFSIZE,
104 parent->i_disk_size);
105 oldpos = 0;
106 while (true) {
107 error = xfs_readdir(sc->tp, parent, &spc.dc, bufsize);
108 if (error)
109 goto out;
110 if (spc.cancelled) {
111 error = -EAGAIN;
112 goto out;
113 }
114 if (oldpos == spc.dc.pos)
115 break;
116 oldpos = spc.dc.pos;
117 }
118 *nlink = spc.nlink;
119out:
120 return error;
121}
122
123
124
125
126
127
128STATIC int
129xchk_parent_validate(
130 struct xfs_scrub *sc,
131 xfs_ino_t dnum,
132 bool *try_again)
133{
134 struct xfs_mount *mp = sc->mp;
135 struct xfs_inode *dp = NULL;
136 xfs_nlink_t expected_nlink;
137 xfs_nlink_t nlink;
138 int error = 0;
139
140 *try_again = false;
141
142 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
143 goto out;
144
145
146 if (sc->ip->i_ino == dnum) {
147 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, 0);
148 goto out;
149 }
150
151
152
153
154
155 expected_nlink = VFS_I(sc->ip)->i_nlink == 0 ? 0 : 1;
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171 error = xfs_iget(mp, sc->tp, dnum, XFS_IGET_UNTRUSTED, 0, &dp);
172 if (error == -EINVAL || error == -ENOENT) {
173 error = -EFSCORRUPTED;
174 xchk_fblock_process_error(sc, XFS_DATA_FORK, 0, &error);
175 goto out;
176 }
177 if (!xchk_fblock_xref_process_error(sc, XFS_DATA_FORK, 0, &error))
178 goto out;
179 if (dp == sc->ip || !S_ISDIR(VFS_I(dp)->i_mode)) {
180 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, 0);
181 goto out_rele;
182 }
183
184
185
186
187
188
189
190
191 if (xfs_ilock_nowait(dp, XFS_IOLOCK_SHARED)) {
192 error = xchk_parent_count_parent_dentries(sc, dp, &nlink);
193 if (!xchk_fblock_xref_process_error(sc, XFS_DATA_FORK, 0,
194 &error))
195 goto out_unlock;
196 if (nlink != expected_nlink)
197 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, 0);
198 goto out_unlock;
199 }
200
201
202
203
204
205
206
207 xfs_iunlock(sc->ip, sc->ilock_flags);
208 sc->ilock_flags = 0;
209 error = xchk_ilock_inverted(dp, XFS_IOLOCK_SHARED);
210 if (error)
211 goto out_rele;
212
213
214 error = xchk_parent_count_parent_dentries(sc, dp, &nlink);
215 if (!xchk_fblock_xref_process_error(sc, XFS_DATA_FORK, 0, &error))
216 goto out_unlock;
217
218
219 xfs_iunlock(dp, XFS_IOLOCK_SHARED);
220 error = xchk_ilock_inverted(sc->ip, XFS_IOLOCK_EXCL);
221 if (error)
222 goto out_rele;
223 sc->ilock_flags = XFS_IOLOCK_EXCL;
224
225
226
227
228
229
230 expected_nlink = VFS_I(sc->ip)->i_nlink == 0 ? 0 : 1;
231
232
233 error = xfs_dir_lookup(sc->tp, sc->ip, &xfs_name_dotdot, &dnum, NULL);
234 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, 0, &error))
235 goto out_rele;
236
237
238 if (dnum != dp->i_ino) {
239 xfs_irele(dp);
240 *try_again = true;
241 return 0;
242 }
243 xfs_irele(dp);
244
245
246
247
248
249 if (nlink != expected_nlink)
250 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, 0);
251 return error;
252
253out_unlock:
254 xfs_iunlock(dp, XFS_IOLOCK_SHARED);
255out_rele:
256 xfs_irele(dp);
257out:
258 return error;
259}
260
261
262int
263xchk_parent(
264 struct xfs_scrub *sc)
265{
266 struct xfs_mount *mp = sc->mp;
267 xfs_ino_t dnum;
268 bool try_again;
269 int tries = 0;
270 int error = 0;
271
272
273
274
275
276 if (!S_ISDIR(VFS_I(sc->ip)->i_mode))
277 return -ENOENT;
278
279
280 if (!xfs_verify_dir_ino(mp, sc->ip->i_ino)) {
281 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, 0);
282 goto out;
283 }
284
285
286
287
288
289
290
291
292 sc->ilock_flags &= ~(XFS_ILOCK_EXCL | XFS_MMAPLOCK_EXCL);
293 xfs_iunlock(sc->ip, XFS_ILOCK_EXCL | XFS_MMAPLOCK_EXCL);
294
295
296 error = xfs_dir_lookup(sc->tp, sc->ip, &xfs_name_dotdot, &dnum, NULL);
297 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, 0, &error))
298 goto out;
299 if (!xfs_verify_dir_ino(mp, dnum)) {
300 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, 0);
301 goto out;
302 }
303
304
305 if (sc->ip == mp->m_rootip) {
306 if (sc->ip->i_ino != mp->m_sb.sb_rootino ||
307 sc->ip->i_ino != dnum)
308 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, 0);
309 goto out;
310 }
311
312 do {
313 error = xchk_parent_validate(sc, dnum, &try_again);
314 if (error)
315 goto out;
316 } while (try_again && ++tries < 20);
317
318
319
320
321
322 if (try_again && tries == 20)
323 xchk_set_incomplete(sc);
324out:
325
326
327
328
329 if ((sc->flags & XCHK_TRY_HARDER) && error == -EDEADLOCK) {
330 error = 0;
331 xchk_set_incomplete(sc);
332 }
333 return error;
334}
335