summaryrefslogtreecommitdiff
path: root/fs/xfs/scrub/parent_repair.c
blob: 28ffcee4f9e5b84a8c6fe24a989b2dff02508dbd (plain)
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Copyright (C) 2021 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_defer.h"
#include "xfs_bit.h"
#include "xfs_log_format.h"
#include "xfs_trans.h"
#include "xfs_sb.h"
#include "xfs_inode.h"
#include "xfs_icache.h"
#include "xfs_da_format.h"
#include "xfs_da_btree.h"
#include "xfs_dir2.h"
#include "xfs_bmap_btree.h"
#include "xfs_dir2_priv.h"
#include "xfs_trans_space.h"
#include "xfs_health.h"
#include "scrub/xfs_scrub.h"
#include "scrub/scrub.h"
#include "scrub/common.h"
#include "scrub/trace.h"
#include "scrub/repair.h"
#include "scrub/iscan.h"
#include "scrub/parent.h"
#include "scrub/orphanage.h"

struct xrep_findparent_info {
	/* The directory currently being scanning, and a readdir context. */
	struct dir_context	dc;
	struct xfs_inode	*dp;

	/*
	 * Scrub context.  We're looking for a @dp containing a directory
	 * entry pointing to sc->ip->i_ino.
	 */
	struct xfs_scrub	*sc;

	/*
	 * Parent that we've found for sc->ip.  If we're scanning the entire
	 * directory tree, we need this to ensure that we only find /one/
	 * parent directory.
	 */
	xfs_ino_t		found_parent;

	/*
	 * Errors encountered during scanning.  Note that the vfs readdir
	 * functions squash the nonzero codes that we return here into a
	 * "short" directory read, so the actual error codes are tracked and
	 * returned separately for simplicity.
	 */
	int			scan_error;
};

/*
 * If this directory entry points to the scrub target inode, then the directory
 * we're scanning is the parent of the scrub target inode.
 */
STATIC int
xrep_findparent_dirent(
	struct dir_context		*dc,
	const char			*name,
	int				namelen,
	loff_t				pos,
	u64				ino,
	unsigned			type)
{
	struct xrep_findparent_info	*fpi;

	fpi = container_of(dc, struct xrep_findparent_info, dc);

	if (xchk_should_terminate(fpi->sc, &fpi->scan_error))
		return 1;

	if (ino != fpi->sc->ip->i_ino)
		return 0;

	/* Should never happen, but we want to bail out regardless. */
	if (namelen == 0) {
		fpi->scan_error = -EFSCORRUPTED;
		return 1;
	}

	/*
	 * Ignore dotdot and dot entries -- we're looking for parent -> child
	 * links only.
	 */
	if (name[0] == '.' && (namelen == 1 ||
				(namelen == 2 && name[1] == '.')))
		return 0;

	/* Uhoh, more than one parent for a dir? */
	if (fpi->found_parent != NULLFSINO) {
		trace_xrep_findparent_dirent(fpi->sc->ip, 0);
		fpi->scan_error = -EFSCORRUPTED;
		return 1;
	}

	/* We found a potential parent; remember this. */
	trace_xrep_findparent_dirent(fpi->sc->ip, fpi->dp->i_ino);
	fpi->found_parent = fpi->dp->i_ino;
	return 0;
}

/*
 * If this is a directory, walk the dirents looking for any that point to the
 * scrub target inode.
 */
STATIC int
xrep_findparent_walk_directory(
	struct xrep_findparent_info	*fpi)
{
	struct xfs_scrub		*sc = fpi->sc;
	struct xfs_inode		*dp = fpi->dp;
	loff_t				oldpos;
	size_t				bufsize;
	unsigned int			lock_mode;
	int				error = 0;

	/* We can't point to ourselves. */
	if (dp == sc->ip)
		return 0;

	/* Don't mix metadata and regular directory trees. */
	if (xfs_is_metadata_inode(dp) ^ xfs_is_metadata_inode(sc->ip))
		return 0;

	/*
	 * If we can take the parent's lock then we're ready to scan.
	 *
	 * If we can't, release the lock on the child, and then try to lock the
	 * alleged parent and child at the same time.  Use trylock for the
	 * second lock so that we don't ABBA deadlock the system.
	 */
	if (!xfs_ilock_nowait(dp, XFS_IOLOCK_SHARED)) {
		xfs_ino_t	orig_parent, new_parent;

		orig_parent = xrep_dotdot_lookup(sc);

		error = xchk_parent_lock_two_dirs(sc, dp);
		if (error)
			return error;

		/*
		 * If the parent changed or the child was unlinked while the
		 * child directory was unlocked, we don't need to do anything
		 * further.
		 */
		new_parent = xrep_dotdot_lookup(sc);
		if (orig_parent != new_parent || VFS_I(sc->ip)->i_nlink == 0) {
			fpi->found_parent = new_parent;
			error = -ECANCELED;
			goto out_unlock;
		}
	}

	/*
	 * If this directory is known to be sick, we cannot scan it reliably
	 * and must abort.
	 */
	if (xfs_inode_has_sickness(dp, XFS_SICK_INO_CORE |
				       XFS_SICK_INO_BMBTD |
				       XFS_SICK_INO_DIR)) {
		error = -EFSCORRUPTED;
		goto out_unlock;
	}

	/*
	 * If there are any blocks, read-ahead block 0 as we're almost certain
	 * to have the next operation be a read there.  This is how we
	 * guarantee that the directory's extent map has been loaded, if there
	 * is one.
	 */
	lock_mode = xfs_ilock_data_map_shared(dp);
	if (dp->i_df.if_nextents > 0)
		error = xfs_dir3_data_readahead(dp, 0, 0);
	xfs_iunlock(dp, lock_mode);
	if (error)
		goto out_unlock;

	/*
	 * Scan the directory to see if there it contains an entry pointing to
	 * the directory that we are repairing.
	 */
	bufsize = (size_t)min_t(loff_t, XFS_READDIR_BUFSIZE, dp->i_disk_size);
	fpi->dc.pos = 0;
	oldpos = 0;
	while (true) {
		error = xfs_readdir(sc->tp, dp, &fpi->dc, bufsize);
		if (error)
			break;
		if (fpi->scan_error) {
			error = fpi->scan_error;
			break;
		}
		if (oldpos == fpi->dc.pos)
			break;
		oldpos = fpi->dc.pos;
	}

out_unlock:
	xfs_iunlock(dp, XFS_IOLOCK_SHARED);
	return error;
}

/*
 * Confirm that the directory @parent_ino actually contains a directory entry
 * pointing to the child @sc->ip->ino.  This function returns one of several
 * ways:
 *
 * Returns 0 with @parent_ino unchanged if the parent was confirmed.
 * Returns 0 with a different @parent_ino if we had to cycle inode locks to
 * walk the alleged parent and the child's '..' entry was changed in the mean
 * time.
 * Returns 0 with @parent_ino set to NULLFSINO if the parent was not valid.
 * Returns the usual negative errno if something else happened.
 */
int
xrep_parent_confirm(
	struct xfs_scrub	*sc,
	xfs_ino_t		*parent_ino)
{
	struct xrep_findparent_info fpi = {
		.sc		= sc,
		.dc.actor	= xrep_findparent_dirent,
		.found_parent	= NULLFSINO,
	};
	int			error;

	/* The root directory always points to itself. */
	if (sc->ip == sc->mp->m_rootip) {
		*parent_ino = sc->mp->m_sb.sb_rootino;
		return 0;
	}

	/* The metadata root directory always points to itself. */
	if (sc->ip == sc->mp->m_metadirip) {
		*parent_ino = sc->mp->m_sb.sb_metadirino;
		return 0;
	}

	/*
	 * Unlinked dirs can point anywhere, so we point them at the root dir
	 * of whichever tree is appropriate.
	 */
	if (VFS_I(sc->ip)->i_nlink == 0) {
		if (xfs_is_metadata_inode(sc->ip))
			*parent_ino = sc->mp->m_sb.sb_metadirino;
		else
			*parent_ino = sc->mp->m_sb.sb_rootino;
		return 0;
	}

	/* Reject garbage parent inode numbers and self-referential parents. */
	if (*parent_ino == NULLFSINO)
	       return 0;
	if (!xfs_verify_dir_ino(sc->mp, *parent_ino) ||
	    *parent_ino == sc->ip->i_ino) {
		*parent_ino = NULLFSINO;
		return 0;
	}

	error = xfs_iget(sc->mp, sc->tp, *parent_ino, XFS_IGET_UNTRUSTED, 0,
			&fpi.dp);
	if (error)
		return error;

	if (!S_ISDIR(VFS_I(fpi.dp)->i_mode)) {
		*parent_ino = NULLFSINO;
		goto out_rele;
	}

	error = xrep_findparent_walk_directory(&fpi);
	if (error == -ECANCELED)
		error = 0;
	if (error)
		goto out_rele;

	*parent_ino = fpi.found_parent;
out_rele:
	xfs_irele(fpi.dp);
	return error;
}

/* Check the dentry cache to see if knows of a parent for the scrub target. */
xfs_ino_t
xrep_parent_from_dcache(
	struct xfs_scrub	*sc)
{
	struct inode		*pip = NULL;
	struct dentry		*dentry, *parent;
	xfs_ino_t		ret = NULLFSINO;

	dentry = d_find_alias(VFS_I(sc->ip));
	if (!dentry)
		goto out;

	parent = dget_parent(dentry);
	if (!parent)
		goto out_dput;

	if (parent->d_sb != sc->ip->i_mount->m_super) {
		dput(parent);
		goto out_dput;
	}

	pip = igrab(d_inode(parent));
	dput(parent);

	if (S_ISDIR(pip->i_mode)) {
		trace_xrep_findparent_from_dcache(sc->ip, XFS_I(pip)->i_ino);
		ret = XFS_I(pip)->i_ino;
	}

	xfs_irele(XFS_I(pip));

out_dput:
	dput(dentry);
out:
	return ret;
}

/*
 * Scan the entire filesystem looking for a parent inode.
 *
 * Returns 0 with @parent_ino set to the parent that we found, or the current
 * value of the child's '..' entry, if it changed when we had to drop the
 * child's IOLOCK.
 * Returns 0 with @parent_ino set to NULLFSINO if we didn't find anything.
 * Returns the usual negative errno if something else happened.
 */
int
xrep_parent_scan(
	struct xfs_scrub		*sc,
	xfs_ino_t			*parent_ino)
{
	struct xrep_findparent_info	fpi = {
		.sc			= sc,
		.dc.actor		= xrep_findparent_dirent,
		.found_parent		= NULLFSINO,
	};
	struct xchk_iscan		iscan = {
		.iget_tries		= 20,
		.iget_retry_delay	= HZ / 10,
	};
	int				ret;

	xchk_iscan_start(&iscan);
	while ((ret = xchk_iscan_advance(sc, &iscan)) == 1) {
		ret = xchk_iscan_iget(sc, &iscan, &fpi.dp);
		if (ret == -EAGAIN)
			continue;
		if (ret)
			break;

		if (S_ISDIR(VFS_I(fpi.dp)->i_mode))
			ret = xrep_findparent_walk_directory(&fpi);
		xchk_iscan_mark_visited(&iscan, fpi.dp);
		xfs_irele(fpi.dp);
		if (ret)
			break;

		if (xchk_should_terminate(sc, &ret))
			break;
	}
	xchk_iscan_finish(&iscan);

	if (ret == -ECANCELED)
		ret = 0;
	if (ret)
		return ret;

	*parent_ino = fpi.found_parent;
	return 0;
}

static inline struct xrep_orphanage_req *
xrep_parent_orphanage_req(
	struct xfs_scrub	*sc)
{
	return sc->buf;
}

static inline unsigned char *
xrep_parent_orphanage_namebuf(
	struct xfs_scrub	*sc)
{
	return (unsigned char *)(((struct xrep_orphanage_req *)sc->buf) + 1);
}

/* Set up for a parent repair. */
int
xrep_setup_parent(
	struct xfs_scrub	*sc)
{
	/* We need a buffer for the orphanage request and a name buffer. */
	sc->buf = kvmalloc(xrep_orphanage_req_sizeof(),
			GFP_KERNEL | __GFP_NOWARN | __GFP_RETRY_MAYFAIL);
	if (!sc->buf)
		return -ENOMEM;

	return xrep_orphanage_try_create(sc);
}

/*
 * Repairing The Directory Parent Pointer
 * ======================================
 *
 * Currently, only directories support parent pointers (in the form of '..'
 * entries), so we simply scan the filesystem and update the '..' entry.
 *
 * Note that because the only parent pointer is the dotdot entry, we won't
 * touch an unhealthy directory, since the directory repair code is perfectly
 * capable of rebuilding a directory with the proper parent inode.
 */

/* Replace a directory's parent '..' pointer. */
STATIC int
xrep_parent_reset_dir(
	struct xfs_scrub	*sc,
	xfs_ino_t		parent_ino)
{
	unsigned int		spaceres;
	int			error;

	trace_xrep_parent_reset_dir(sc->ip, parent_ino);

	/* Reserve more space just in case we have to expand the dir. */
	spaceres = XFS_RENAME_SPACE_RES(sc->mp, 2);
	error = xfs_trans_reserve_more_inode(sc->tp, sc->ip, spaceres, 0);
	if (error)
		return error;

	/* Replace the dotdot entry. */
	return xfs_dir_replace(sc->tp, sc->ip, &xfs_name_dotdot, parent_ino,
			spaceres);
}

/*
 * Move the current file to the orphanage.  Caller must not hold any inode
 * locks.  Upon return, the scrub state will reflect the transaction, ijoin,
 * and inode lock states.
 */
STATIC int
xrep_parent_move_to_orphanage(
	struct xfs_scrub	*sc)
{
	struct xrep_orphanage_req *orph = xrep_parent_orphanage_req(sc);
	unsigned char		*namebuf = xrep_parent_orphanage_namebuf(sc);
	int			error;

	/* No orphanage?  We can't fix this. */
	if (!sc->orphanage)
		return -EFSCORRUPTED;

	/*
	 * If we can take the orphanage's iolock then we're ready to move.
	 *
	 * If we can't, release the iolock on the child, and then try to iolock
	 * the orphanage and child at the same time.  Use trylock for the
	 * second lock so that we don't ABBA deadlock the system.
	 */
	if (!xrep_orphanage_ilock_nowait(sc, XFS_IOLOCK_EXCL)) {
		xfs_ino_t	orig_parent, new_parent;

		orig_parent = xrep_dotdot_lookup(sc);

		xchk_iunlock(sc, sc->ilock_flags);
		error = xrep_orphanage_iolock_two(sc);
		if (error)
			return error;

		/*
		 * If the parent changed or the child was unlinked while the
		 * child directory was unlocked, we don't need to move the
		 * child to the orphanage after all.
		 */
		new_parent = xrep_dotdot_lookup(sc);
		if (orig_parent != new_parent || VFS_I(sc->ip)->i_nlink == 0)
			return 0;
	}

	/*
	 * Move the directory to the orphanage, and let scrub teardown unlock
	 * everything for us.
	 */
	xrep_orphanage_compute_blkres(sc, orph);

	error = xrep_orphanage_compute_name(orph, namebuf);
	if (error)
		return error;

	error = xfs_trans_reserve_more(sc->tp,
			orph->orphanage_blkres + orph->child_blkres, 0);
	if (error)
		return error;

	error = xrep_orphanage_ilock_resv_quota(orph);
	if (error)
		return error;

	return xrep_orphanage_adopt(orph);
}

int
xrep_parent(
	struct xfs_scrub	*sc)
{
	xfs_ino_t		parent_ino, curr_parent;
	unsigned int		sick, checked;
	int			error;

	/*
	 * Avoid sick directories.  The parent pointer scrubber dropped the
	 * ILOCK and MMAPLOCK, but we still hold IOLOCK_EXCL on the directory.
	 * There shouldn't be anyone else clearing the directory's sick status.
	 */
	xfs_inode_measure_sickness(sc->ip, &sick, &checked);
	if (sick & XFS_SICK_INO_DIR)
		return -EFSCORRUPTED;

	/* Does the VFS dcache have an answer for us? */
	parent_ino = xrep_parent_from_dcache(sc);
	error = xrep_parent_confirm(sc, &parent_ino);
	if (!error && parent_ino != NULLFSINO)
		goto reset_parent;

	/* Scan the entire filesystem for a parent. */
	error = xrep_parent_scan(sc, &parent_ino);
	if (error)
		return error;
	if (parent_ino == NULLFSINO)
		return xrep_parent_move_to_orphanage(sc);

reset_parent:
	/* If the '..' entry is already set to the parent inode, we're done. */
	curr_parent = xrep_dotdot_lookup(sc);
	if (curr_parent != NULLFSINO && curr_parent == parent_ino)
		return 0;

	/* Re-take the ILOCK, we're going to need it to modify the dir. */
	xchk_ilock(sc, XFS_ILOCK_EXCL);
	xfs_trans_ijoin(sc->tp, sc->ip, 0);

	error = xrep_ino_dqattach(sc);
	if (error)
		return error;

	return xrep_parent_reset_dir(sc, parent_ino);
}