xref: /linux/fs/xfs/scrub/symlink.c (revision eeb9f5c2dcec90009d7cf12e780e7f9631993fc5)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2017-2023 Oracle.  All Rights Reserved.
4  * Author: Darrick J. Wong <djwong@kernel.org>
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_symlink.h"
15 #include "xfs_health.h"
16 #include "scrub/scrub.h"
17 #include "scrub/common.h"
18 #include "scrub/health.h"
19 
20 /* Set us up to scrub a symbolic link. */
21 int
22 xchk_setup_symlink(
23 	struct xfs_scrub	*sc)
24 {
25 	/* Allocate the buffer without the inode lock held. */
26 	sc->buf = kvzalloc(XFS_SYMLINK_MAXLEN + 1, XCHK_GFP_FLAGS);
27 	if (!sc->buf)
28 		return -ENOMEM;
29 
30 	return xchk_setup_inode_contents(sc, 0);
31 }
32 
33 /* Symbolic links. */
34 
35 int
36 xchk_symlink(
37 	struct xfs_scrub	*sc)
38 {
39 	struct xfs_inode	*ip = sc->ip;
40 	struct xfs_ifork	*ifp;
41 	loff_t			len;
42 	int			error = 0;
43 
44 	if (!S_ISLNK(VFS_I(ip)->i_mode))
45 		return -ENOENT;
46 
47 	if (xchk_file_looks_zapped(sc, XFS_SICK_INO_SYMLINK_ZAPPED)) {
48 		xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, 0);
49 		return 0;
50 	}
51 
52 	ifp = xfs_ifork_ptr(ip, XFS_DATA_FORK);
53 	len = ip->i_disk_size;
54 
55 	/* Plausible size? */
56 	if (len > XFS_SYMLINK_MAXLEN || len <= 0) {
57 		xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, 0);
58 		return 0;
59 	}
60 
61 	/* Inline symlink? */
62 	if (ifp->if_format == XFS_DINODE_FMT_LOCAL) {
63 		if (len > xfs_inode_data_fork_size(ip) ||
64 		    len > strnlen(ifp->if_data, xfs_inode_data_fork_size(ip)))
65 			xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, 0);
66 		return 0;
67 	}
68 
69 	/* Remote symlink; must read the contents. */
70 	error = xfs_readlink_bmap_ilocked(sc->ip, sc->buf);
71 	if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, 0, &error))
72 		return error;
73 	if (strnlen(sc->buf, XFS_SYMLINK_MAXLEN) < len)
74 		xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, 0);
75 
76 	/* If a remote symlink is clean, it is clearly not zapped. */
77 	xchk_mark_healthy_if_clean(sc, XFS_SICK_INO_SYMLINK_ZAPPED);
78 	return 0;
79 }
80