|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | +/* |
| 3 | + * Copyright (c) 2023-2024 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_trans.h" |
| 14 | +#include "xfs_inode.h" |
| 15 | +#include "xfs_metafile.h" |
| 16 | +#include "xfs_quota.h" |
| 17 | +#include "xfs_qm.h" |
| 18 | +#include "xfs_dir2.h" |
| 19 | +#include "scrub/scrub.h" |
| 20 | +#include "scrub/common.h" |
| 21 | +#include "scrub/trace.h" |
| 22 | +#include "scrub/readdir.h" |
| 23 | + |
| 24 | +/* |
| 25 | + * Metadata Directory Tree Paths |
| 26 | + * ============================= |
| 27 | + * |
| 28 | + * A filesystem with metadir enabled expects to find metadata structures |
| 29 | + * attached to files that are accessible by walking a path down the metadata |
| 30 | + * directory tree. Given the metadir path and the incore inode storing the |
| 31 | + * metadata, this scrubber ensures that the ondisk metadir path points to the |
| 32 | + * ondisk inode represented by the incore inode. |
| 33 | + */ |
| 34 | + |
| 35 | +struct xchk_metapath { |
| 36 | + struct xfs_scrub *sc; |
| 37 | + |
| 38 | + /* Name for lookup */ |
| 39 | + struct xfs_name xname; |
| 40 | + |
| 41 | + /* Path for this metadata file and the parent directory */ |
| 42 | + const char *path; |
| 43 | + const char *parent_path; |
| 44 | + |
| 45 | + /* Directory parent of the metadata file. */ |
| 46 | + struct xfs_inode *dp; |
| 47 | + |
| 48 | + /* Locks held on dp */ |
| 49 | + unsigned int dp_ilock_flags; |
| 50 | +}; |
| 51 | + |
| 52 | +/* Release resources tracked in the buffer. */ |
| 53 | +static inline void |
| 54 | +xchk_metapath_cleanup( |
| 55 | + void *buf) |
| 56 | +{ |
| 57 | + struct xchk_metapath *mpath = buf; |
| 58 | + |
| 59 | + if (mpath->dp_ilock_flags) |
| 60 | + xfs_iunlock(mpath->dp, mpath->dp_ilock_flags); |
| 61 | + kfree(mpath->path); |
| 62 | +} |
| 63 | + |
| 64 | +int |
| 65 | +xchk_setup_metapath( |
| 66 | + struct xfs_scrub *sc) |
| 67 | +{ |
| 68 | + if (!xfs_has_metadir(sc->mp)) |
| 69 | + return -ENOENT; |
| 70 | + if (sc->sm->sm_gen) |
| 71 | + return -EINVAL; |
| 72 | + |
| 73 | + switch (sc->sm->sm_ino) { |
| 74 | + case XFS_SCRUB_METAPATH_PROBE: |
| 75 | + /* Just probing, nothing else to do. */ |
| 76 | + if (sc->sm->sm_agno) |
| 77 | + return -EINVAL; |
| 78 | + return 0; |
| 79 | + default: |
| 80 | + return -ENOENT; |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +/* |
| 85 | + * Take the ILOCK on the metadata directory parent and child. We do not know |
| 86 | + * that the metadata directory is not corrupt, so we lock the parent and try |
| 87 | + * to lock the child. Returns 0 if successful, or -EINTR to abort the scrub. |
| 88 | + */ |
| 89 | +STATIC int |
| 90 | +xchk_metapath_ilock_both( |
| 91 | + struct xchk_metapath *mpath) |
| 92 | +{ |
| 93 | + struct xfs_scrub *sc = mpath->sc; |
| 94 | + int error = 0; |
| 95 | + |
| 96 | + while (true) { |
| 97 | + xfs_ilock(mpath->dp, XFS_ILOCK_EXCL); |
| 98 | + if (xchk_ilock_nowait(sc, XFS_ILOCK_EXCL)) { |
| 99 | + mpath->dp_ilock_flags |= XFS_ILOCK_EXCL; |
| 100 | + return 0; |
| 101 | + } |
| 102 | + xfs_iunlock(mpath->dp, XFS_ILOCK_EXCL); |
| 103 | + |
| 104 | + if (xchk_should_terminate(sc, &error)) |
| 105 | + return error; |
| 106 | + |
| 107 | + delay(1); |
| 108 | + } |
| 109 | + |
| 110 | + ASSERT(0); |
| 111 | + return -EINTR; |
| 112 | +} |
| 113 | + |
| 114 | +/* Unlock parent and child inodes. */ |
| 115 | +static inline void |
| 116 | +xchk_metapath_iunlock( |
| 117 | + struct xchk_metapath *mpath) |
| 118 | +{ |
| 119 | + struct xfs_scrub *sc = mpath->sc; |
| 120 | + |
| 121 | + xchk_iunlock(sc, XFS_ILOCK_EXCL); |
| 122 | + |
| 123 | + mpath->dp_ilock_flags &= ~XFS_ILOCK_EXCL; |
| 124 | + xfs_iunlock(mpath->dp, XFS_ILOCK_EXCL); |
| 125 | +} |
| 126 | + |
| 127 | +int |
| 128 | +xchk_metapath( |
| 129 | + struct xfs_scrub *sc) |
| 130 | +{ |
| 131 | + struct xchk_metapath *mpath = sc->buf; |
| 132 | + xfs_ino_t ino = NULLFSINO; |
| 133 | + int error; |
| 134 | + |
| 135 | + /* Just probing, nothing else to do. */ |
| 136 | + if (sc->sm->sm_ino == XFS_SCRUB_METAPATH_PROBE) |
| 137 | + return 0; |
| 138 | + |
| 139 | + /* Parent required to do anything else. */ |
| 140 | + if (mpath->dp == NULL) { |
| 141 | + xchk_ino_set_corrupt(sc, sc->ip->i_ino); |
| 142 | + return 0; |
| 143 | + } |
| 144 | + |
| 145 | + error = xchk_trans_alloc_empty(sc); |
| 146 | + if (error) |
| 147 | + return error; |
| 148 | + |
| 149 | + error = xchk_metapath_ilock_both(mpath); |
| 150 | + if (error) |
| 151 | + goto out_cancel; |
| 152 | + |
| 153 | + /* Make sure the parent dir has a dirent pointing to this file. */ |
| 154 | + error = xchk_dir_lookup(sc, mpath->dp, &mpath->xname, &ino); |
| 155 | + trace_xchk_metapath_lookup(sc, mpath->path, mpath->dp, ino); |
| 156 | + if (error == -ENOENT) { |
| 157 | + /* No directory entry at all */ |
| 158 | + xchk_ino_set_corrupt(sc, sc->ip->i_ino); |
| 159 | + error = 0; |
| 160 | + goto out_ilock; |
| 161 | + } |
| 162 | + if (!xchk_fblock_xref_process_error(sc, XFS_DATA_FORK, 0, &error)) |
| 163 | + goto out_ilock; |
| 164 | + if (ino != sc->ip->i_ino) { |
| 165 | + /* Pointing to wrong inode */ |
| 166 | + xchk_ino_set_corrupt(sc, sc->ip->i_ino); |
| 167 | + } |
| 168 | + |
| 169 | +out_ilock: |
| 170 | + xchk_metapath_iunlock(mpath); |
| 171 | +out_cancel: |
| 172 | + xchk_trans_cancel(sc); |
| 173 | + return error; |
| 174 | +} |
0 commit comments