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