10b61f8a4SDave Chinner// SPDX-License-Identifier: GPL-2.0+
22a721dbbSDarrick J. Wong/*
32a721dbbSDarrick J. Wong * Copyright (C) 2017 Oracle.  All Rights Reserved.
42a721dbbSDarrick J. Wong * Author: Darrick J. Wong <darrick.wong@oracle.com>
52a721dbbSDarrick J. Wong */
62a721dbbSDarrick J. Wong#include "xfs.h"
72a721dbbSDarrick J. Wong#include "xfs_fs.h"
82a721dbbSDarrick J. Wong#include "xfs_shared.h"
92a721dbbSDarrick J. Wong#include "xfs_format.h"
102a721dbbSDarrick J. Wong#include "xfs_trans_resv.h"
112a721dbbSDarrick J. Wong#include "xfs_mount.h"
122a721dbbSDarrick J. Wong#include "xfs_log_format.h"
132a721dbbSDarrick J. Wong#include "xfs_inode.h"
142a721dbbSDarrick J. Wong#include "xfs_symlink.h"
152a721dbbSDarrick J. Wong#include "scrub/scrub.h"
162a721dbbSDarrick J. Wong#include "scrub/common.h"
172a721dbbSDarrick J. Wong
182a721dbbSDarrick J. Wong/* Set us up to scrub a symbolic link. */
192a721dbbSDarrick J. Wongint
20c517b3aaSDarrick J. Wongxchk_setup_symlink(
21026f57ebSDarrick J. Wong	struct xfs_scrub	*sc)
222a721dbbSDarrick J. Wong{
232a721dbbSDarrick J. Wong	/* Allocate the buffer without the inode lock held. */
248ca79df8SCarlos Maiolino	sc->buf = kvzalloc(XFS_SYMLINK_MAXLEN + 1, GFP_KERNEL);
252a721dbbSDarrick J. Wong	if (!sc->buf)
262a721dbbSDarrick J. Wong		return -ENOMEM;
272a721dbbSDarrick J. Wong
28026f57ebSDarrick J. Wong	return xchk_setup_inode_contents(sc, 0);
292a721dbbSDarrick J. Wong}
302a721dbbSDarrick J. Wong
312a721dbbSDarrick J. Wong/* Symbolic links. */
322a721dbbSDarrick J. Wong
332a721dbbSDarrick J. Wongint
34c517b3aaSDarrick J. Wongxchk_symlink(
351d8a748aSDarrick J. Wong	struct xfs_scrub	*sc)
362a721dbbSDarrick J. Wong{
37032d91f9SDarrick J. Wong	struct xfs_inode	*ip = sc->ip;
38032d91f9SDarrick J. Wong	struct xfs_ifork	*ifp;
39032d91f9SDarrick J. Wong	loff_t			len;
40032d91f9SDarrick J. Wong	int			error = 0;
412a721dbbSDarrick J. Wong
422a721dbbSDarrick J. Wong	if (!S_ISLNK(VFS_I(ip)->i_mode))
432a721dbbSDarrick J. Wong		return -ENOENT;
442a721dbbSDarrick J. Wong	ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
4513d2c10bSChristoph Hellwig	len = ip->i_disk_size;
462a721dbbSDarrick J. Wong
472a721dbbSDarrick J. Wong	/* Plausible size? */
482a721dbbSDarrick J. Wong	if (len > XFS_SYMLINK_MAXLEN || len <= 0) {
49c517b3aaSDarrick J. Wong		xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, 0);
502a721dbbSDarrick J. Wong		goto out;
512a721dbbSDarrick J. Wong	}
522a721dbbSDarrick J. Wong
532a721dbbSDarrick J. Wong	/* Inline symlink? */
540779f4a6SChristoph Hellwig	if (ifp->if_format == XFS_DINODE_FMT_LOCAL) {
552a721dbbSDarrick J. Wong		if (len > XFS_IFORK_DSIZE(ip) ||
562a721dbbSDarrick J. Wong		    len > strnlen(ifp->if_u1.if_data, XFS_IFORK_DSIZE(ip)))
57c517b3aaSDarrick J. Wong			xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, 0);
582a721dbbSDarrick J. Wong		goto out;
592a721dbbSDarrick J. Wong	}
602a721dbbSDarrick J. Wong
612a721dbbSDarrick J. Wong	/* Remote symlink; must read the contents. */
622a721dbbSDarrick J. Wong	error = xfs_readlink_bmap_ilocked(sc->ip, sc->buf);
63c517b3aaSDarrick J. Wong	if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, 0, &error))
642a721dbbSDarrick J. Wong		goto out;
652a721dbbSDarrick J. Wong	if (strnlen(sc->buf, XFS_SYMLINK_MAXLEN) < len)
66c517b3aaSDarrick J. Wong		xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, 0);
672a721dbbSDarrick J. Wongout:
682a721dbbSDarrick J. Wong	return error;
692a721dbbSDarrick J. Wong}