#include "xfs_platform.h"
#include "xfs_fs.h"
#include "xfs_shared.h"
#include "xfs_format.h"
#include "xfs_trans_resv.h"
#include "xfs_mount.h"
#include "xfs_log_format.h"
#include "xfs_trans.h"
#include "xfs_inode.h"
#include "xfs_icache.h"
#include "xfs_bmap_util.h"
#include "xfs_iwalk.h"
#include "xfs_ialloc.h"
#include "xfs_sb.h"
#include "xfs_ag.h"
#include "xfs_dir2.h"
#include "xfs_parent.h"
#include "scrub/scrub.h"
#include "scrub/common.h"
#include "scrub/repair.h"
#include "scrub/xfile.h"
#include "scrub/xfarray.h"
#include "scrub/iscan.h"
#include "scrub/orphanage.h"
#include "scrub/nlinks.h"
#include "scrub/trace.h"
#include "scrub/tempfile.h"
int
xrep_setup_nlinks(
struct xfs_scrub *sc)
{
return xrep_orphanage_try_create(sc);
}
static inline bool
xrep_nlinks_is_orphaned(
struct xfs_scrub *sc,
struct xfs_inode *ip,
unsigned int actual_nlink,
const struct xchk_nlink *obs)
{
if (obs->parents != 0)
return false;
if (xchk_inode_is_dirtree_root(ip) || ip == sc->orphanage)
return false;
return actual_nlink != 0;
}
STATIC int
xrep_nlinks_iunlink_remove(
struct xfs_scrub *sc)
{
struct xfs_perag *pag;
int error;
pag = xfs_perag_get(sc->mp, XFS_INO_TO_AGNO(sc->mp, sc->ip->i_ino));
error = xfs_iunlink_remove(sc->tp, pag, sc->ip);
xfs_perag_put(pag);
return error;
}
STATIC int
xrep_nlinks_repair_inode(
struct xchk_nlink_ctrs *xnc)
{
struct xchk_nlink obs;
struct xfs_scrub *sc = xnc->sc;
struct xfs_mount *mp = sc->mp;
struct xfs_inode *ip = sc->ip;
uint64_t total_links;
uint64_t actual_nlink;
bool orphanage_available = false;
bool dirty = false;
int error;
if (xrep_is_tempfile(ip))
return 0;
if (xrep_orphanage_can_adopt(sc)) {
error = xrep_orphanage_iolock_two(sc);
if (error)
return error;
error = xrep_adoption_trans_alloc(sc, &xnc->adoption);
if (error) {
xchk_iunlock(sc, XFS_IOLOCK_EXCL);
xrep_orphanage_iunlock(sc, XFS_IOLOCK_EXCL);
} else {
orphanage_available = true;
}
}
if (!orphanage_available) {
xchk_ilock(sc, XFS_IOLOCK_EXCL);
error = xfs_trans_alloc(mp, &M_RES(mp)->tr_link, 0, 0, 0,
&sc->tp);
if (error) {
xchk_iunlock(sc, XFS_IOLOCK_EXCL);
return error;
}
xchk_ilock(sc, XFS_ILOCK_EXCL);
xfs_trans_ijoin(sc->tp, ip, 0);
}
mutex_lock(&xnc->lock);
if (xchk_iscan_aborted(&xnc->collect_iscan)) {
error = -ECANCELED;
goto out_scanlock;
}
error = xfarray_load_sparse(xnc->nlinks, ip->i_ino, &obs);
if (error)
goto out_scanlock;
mutex_unlock(&xnc->lock);
total_links = xchk_nlink_total(ip, &obs);
actual_nlink = VFS_I(ip)->i_nlink;
if (!S_ISDIR(VFS_I(ip)->i_mode) && obs.children != 0) {
trace_xrep_nlinks_unfixable_inode(mp, ip, &obs);
error = 0;
goto out_trans;
}
if (orphanage_available &&
xrep_nlinks_is_orphaned(sc, ip, actual_nlink, &obs)) {
error = xrep_adoption_compute_name(&xnc->adoption, &xnc->xname);
if (error)
goto out_trans;
error = xrep_adoption_move(&xnc->adoption);
if (error)
goto out_trans;
mutex_lock(&xnc->lock);
error = xfarray_load_sparse(xnc->nlinks, ip->i_ino, &obs);
mutex_unlock(&xnc->lock);
if (error)
goto out_trans;
total_links = xchk_nlink_total(ip, &obs);
actual_nlink = VFS_I(ip)->i_nlink;
dirty = true;
}
if (total_links > 0 && xfs_inode_on_unlinked_list(ip)) {
error = xrep_nlinks_iunlink_remove(sc);
if (error)
goto out_trans;
dirty = true;
}
if (total_links == 0 && !xfs_inode_on_unlinked_list(ip)) {
error = xfs_iunlink(sc->tp, ip);
if (error)
goto out_trans;
dirty = true;
}
if (total_links != actual_nlink) {
trace_xrep_nlinks_update_inode(mp, ip, &obs);
set_nlink(VFS_I(ip), min_t(unsigned long long, total_links,
XFS_NLINK_PINNED));
dirty = true;
}
if (!dirty) {
error = 0;
goto out_trans;
}
xfs_trans_log_inode(sc->tp, ip, XFS_ILOG_CORE);
error = xrep_trans_commit(sc);
goto out_unlock;
out_scanlock:
mutex_unlock(&xnc->lock);
out_trans:
xchk_trans_cancel(sc);
out_unlock:
xchk_iunlock(sc, XFS_ILOCK_EXCL);
if (orphanage_available) {
xrep_orphanage_iunlock(sc, XFS_ILOCK_EXCL);
xrep_orphanage_iunlock(sc, XFS_IOLOCK_EXCL);
}
xchk_iunlock(sc, XFS_IOLOCK_EXCL);
return error;
}
static int
xrep_nlinks_iter(
struct xchk_nlink_ctrs *xnc,
struct xfs_inode **ipp)
{
int error;
do {
error = xchk_iscan_iter(&xnc->compare_iscan, ipp);
} while (error == -EBUSY);
return error;
}
int
xrep_nlinks(
struct xfs_scrub *sc)
{
struct xchk_nlink_ctrs *xnc = sc->buf;
int error;
if (!xfs_has_ftype(sc->mp))
return -EOPNOTSUPP;
xchk_iscan_start(sc, 30000, 100, &xnc->compare_iscan);
ASSERT(sc->ip == NULL);
while ((error = xrep_nlinks_iter(xnc, &sc->ip)) == 1) {
xchk_trans_cancel(sc);
error = xrep_nlinks_repair_inode(xnc);
xchk_iscan_mark_visited(&xnc->compare_iscan, sc->ip);
xchk_irele(sc, sc->ip);
sc->ip = NULL;
if (error)
break;
if (xchk_should_terminate(sc, &error))
break;
xchk_trans_alloc_empty(sc);
}
xchk_iscan_iter_finish(&xnc->compare_iscan);
xchk_iscan_teardown(&xnc->compare_iscan);
return error;
}