#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/avl.h>
#define _KERNEL
#include <sys/fs/ufs_fsdir.h>
#undef _KERNEL
#include <sys/debug.h>
#include "fsck.h"
#define OFFSETOF(type, elt) ((size_t)(&((type *)NULL)->elt))
typedef struct claimant {
fsck_ino_t cl_inode;
daddr32_t cl_lfn;
avl_node_t cl_avl;
} claimant_t;
typedef struct fragment {
daddr32_t fr_pfn;
avl_tree_t fr_claimants;
avl_node_t fr_avl;
} fragment_t;
typedef struct reference {
daddr32_t ref_lfn;
daddr32_t ref_pfn;
avl_node_t ref_avl;
} reference_t;
typedef struct inode_dup {
fsck_ino_t id_ino;
avl_tree_t id_fragments;
avl_node_t id_avl;
} inode_dup_t;
static avl_tree_t dup_frags;
static void free_invert_frags(avl_tree_t *);
static void report_dup_lfn_pfn(daddr32_t, daddr32_t, daddr32_t, daddr32_t);
static inode_dup_t *new_inode_dup(fsck_ino_t);
static void invert_frags(avl_tree_t *, avl_tree_t *);
static void report_inode_dups(inode_dup_t *);
static int by_ino_cmp(const void *, const void *);
static int by_lfn_cmp(const void *, const void *);
static claimant_t *alloc_claimant(fsck_ino_t, daddr32_t);
static fragment_t *alloc_dup(daddr32_t);
static int claimant_cmp(const void *, const void *);
static int fragment_cmp(const void *, const void *);
static int decrement_claimant(fragment_t *, fsck_ino_t, daddr32_t);
static int increment_claimant(fragment_t *, fsck_ino_t, daddr32_t);
int
have_dups(void)
{
return (avl_numnodes(&dup_frags) > 0);
}
int
find_dup_ref(daddr32_t fragno, fsck_ino_t ino, daddr32_t lfn, int flags)
{
fragment_t key;
fragment_t *dup;
avl_index_t where;
int added = 0;
int removed = 0;
if (avl_first(&dup_frags) == NULL) {
if (flags & DB_CREATE)
avl_create(&dup_frags, fragment_cmp,
sizeof (fragment_t),
OFFSETOF(fragment_t, fr_avl));
else
return (0);
}
key.fr_pfn = fragno;
dup = avl_find(&dup_frags, (void *)&key, &where);
if ((dup == NULL) & (flags & DB_CREATE)) {
dup = alloc_dup(fragno);
avl_insert(&dup_frags, (void *)dup, where);
}
if (dup != NULL) {
if (flags & DB_INCR) {
if (debug)
(void) printf(
"adding claim by ino %d as lfn %d\n",
ino, lfn);
added = increment_claimant(dup, ino, lfn);
} else if (flags & DB_DECR) {
removed = decrement_claimant(dup, ino, lfn);
if (debug)
(void) printf(
"check for claimant ino %d lfn %d returned %d\n",
ino, lfn, removed);
}
}
return (added || removed || (dup != NULL));
}
int
report_dups(int quiet)
{
int overlaps;
inode_dup_t *inode;
fragment_t *dup;
avl_tree_t inode_frags;
overlaps = 0;
ASSERT(have_dups());
dup = avl_first(&dup_frags);
while (dup != NULL) {
if (avl_numnodes(&dup->fr_claimants) > 1) {
overlaps++;
break;
}
dup = AVL_NEXT(&dup_frags, dup);
}
if (!quiet) {
(void) puts("\nSome blocks that were found to be in "
"multiple files are still\nassigned to "
"file(s).\nFragments sorted by inode and "
"logical offsets:");
invert_frags(&dup_frags, &inode_frags);
inode = avl_first(&inode_frags);
while (inode != NULL) {
report_inode_dups(inode);
inode = AVL_NEXT(&inode_frags, inode);
}
(void) printf("\n");
free_invert_frags(&inode_frags);
}
return (overlaps);
}
static void
report_inode_dups(inode_dup_t *inode)
{
reference_t *dup;
daddr32_t first_lfn, last_lfn, first_pfn, last_pfn;
(void) printf("Inode %d:\n", inode->id_ino);
dup = avl_first(&inode->id_fragments);
first_lfn = last_lfn = dup->ref_lfn;
first_pfn = last_pfn = dup->ref_pfn;
while ((dup = AVL_NEXT(&inode->id_fragments, dup)) != NULL) {
if (((last_lfn + 1) != dup->ref_lfn) ||
((last_pfn + 1) != dup->ref_pfn)) {
report_dup_lfn_pfn(first_lfn, last_lfn,
first_pfn, last_pfn);
first_lfn = last_lfn = dup->ref_lfn;
first_pfn = last_pfn = dup->ref_pfn;
}
}
report_dup_lfn_pfn(first_lfn, last_lfn, first_pfn, last_pfn);
}
static void
report_dup_lfn_pfn(daddr32_t first_lfn, daddr32_t last_lfn,
daddr32_t first_pfn, daddr32_t last_pfn)
{
if ((first_lfn == last_lfn) && (first_pfn == last_pfn)) {
(void) printf(
" Logical Offset 0x%08llx Physical Fragment %d\n",
(longlong_t)first_lfn * sblock.fs_fsize, first_pfn);
} else {
(void) printf(
" Logical Offsets 0x%08llx - 0x%08llx, "
"Physical Fragments %d - %d\n",
(longlong_t)first_lfn * sblock.fs_fsize,
(longlong_t)last_lfn * sblock.fs_fsize,
first_pfn, last_pfn);
}
}
static void
invert_frags(avl_tree_t *source, avl_tree_t *target)
{
fragment_t *src_frag;
claimant_t *src_claim;
inode_dup_t *tgt_inode;
inode_dup_t tgt_inode_key;
reference_t *tgt_ref;
reference_t tgt_ref_key;
avl_index_t where;
avl_create(target, by_ino_cmp, sizeof (inode_dup_t),
OFFSETOF(inode_dup_t, id_avl));
src_frag = avl_first(source);
while (src_frag != NULL) {
src_claim = avl_first(&src_frag->fr_claimants);
while (src_claim != NULL) {
tgt_inode_key.id_ino = src_claim->cl_inode;
tgt_inode = avl_find(target, (void *)&tgt_inode_key,
&where);
if (tgt_inode == NULL) {
tgt_inode = new_inode_dup(src_claim->cl_inode);
avl_insert(target, (void *)tgt_inode, where);
}
tgt_ref_key.ref_lfn = src_claim->cl_lfn;
tgt_ref = avl_find(&tgt_inode->id_fragments,
(void *)&tgt_ref_key, &where);
if (tgt_ref == NULL) {
tgt_ref = (reference_t *)malloc(
sizeof (reference_t));
if (tgt_ref == NULL)
errexit("Out of memory in "
"invert_frags\n");
tgt_ref->ref_lfn = src_claim->cl_lfn;
tgt_ref->ref_pfn = src_frag->fr_pfn;
avl_insert(&tgt_inode->id_fragments,
(void *)tgt_ref, where);
}
src_claim = AVL_NEXT(&src_frag->fr_claimants,
src_claim);
}
src_frag = AVL_NEXT(source, src_frag);
}
}
static void
free_invert_frags(avl_tree_t *tree)
{
void *outer = NULL;
void *inner;
inode_dup_t *inode_dup;
reference_t *ref_dup;
while ((inode_dup = avl_destroy_nodes(tree, &outer)) != NULL) {
inner = NULL;
while ((ref_dup = avl_destroy_nodes(&inode_dup->id_fragments,
&inner)) != NULL) {
free((void *)ref_dup);
}
avl_destroy(&inode_dup->id_fragments);
free((void *)inode_dup);
}
avl_destroy(tree);
}
void
free_dup_state(void)
{
void *dup_cookie = NULL;
void *claim_cookie;
fragment_t *fragv;
claimant_t *claimv;
while ((fragv = avl_destroy_nodes(&dup_frags, &dup_cookie)) != NULL) {
claim_cookie = NULL;
while ((claimv = avl_destroy_nodes(&fragv->fr_claimants,
&claim_cookie)) != NULL) {
free((void *)claimv);
}
avl_destroy(&fragv->fr_claimants);
free((void *)fragv);
}
avl_destroy(&dup_frags);
}
static int
increment_claimant(fragment_t *dup, fsck_ino_t ino, daddr32_t lfn)
{
avl_index_t where;
claimant_t *claimant;
claimant_t key;
int added = 0;
key.cl_inode = ino;
key.cl_lfn = lfn;
claimant = avl_find(&dup->fr_claimants, &key, &where);
if (claimant == NULL) {
if (debug)
(void) printf("inserting claimant\n");
claimant = alloc_claimant(ino, lfn);
avl_insert(&dup->fr_claimants, (void *)claimant, where);
statemap[ino] |= INCLEAR;
if (statemap[ino] & INZLINK) {
statemap[ino] &= ~INZLINK;
if (statemap[ino] & DSTATE) {
add_orphan_dir(ino);
}
}
added = 1;
}
return (added);
}
static int
decrement_claimant(fragment_t *dup, fsck_ino_t ino, daddr32_t lfn)
{
avl_index_t where;
claimant_t *claimant;
claimant_t key;
int busy = 0;
key.cl_inode = ino;
key.cl_lfn = lfn;
claimant = avl_find(&dup->fr_claimants, &key, &where);
if (claimant != NULL) {
avl_remove(&dup->fr_claimants, claimant);
if (avl_numnodes(&dup->fr_claimants) == 0) {
avl_destroy(&dup->fr_claimants);
avl_remove(&dup_frags, (void *)dup);
free((void *)dup);
} else {
busy = 1;
}
}
return (busy);
}
static claimant_t *
alloc_claimant(fsck_ino_t inode, daddr32_t lfn)
{
claimant_t *new = (claimant_t *)malloc(sizeof (claimant_t));
if (new == NULL)
errexit("Out of memory in alloc_claimant()\n");
new->cl_inode = inode;
new->cl_lfn = lfn;
return (new);
}
static fragment_t *
alloc_dup(daddr32_t pfn)
{
fragment_t *new = (fragment_t *)malloc(sizeof (fragment_t));
if (new == NULL)
errexit("Out of memory in alloc_dup()\n");
new->fr_pfn = pfn;
avl_create(&new->fr_claimants, claimant_cmp, sizeof (fragment_t),
OFFSETOF(claimant_t, cl_avl));
return (new);
}
static int
fragment_cmp(const void *vlp, const void *vrp)
{
const fragment_t *lp = (const fragment_t *)vlp;
const fragment_t *rp = (const fragment_t *)vrp;
int cmp = lp->fr_pfn - rp->fr_pfn;
if (cmp < 0)
cmp = -1;
else if (cmp > 0)
cmp = 1;
return (cmp);
}
static int
claimant_cmp(const void *vlp, const void *vrp)
{
const claimant_t *lp = (const claimant_t *)vlp;
const claimant_t *rp = (const claimant_t *)vrp;
int cmp;
cmp = lp->cl_inode - rp->cl_inode;
if (cmp == 0) {
if ((lp->cl_lfn >= 0) && (rp->cl_lfn >= 0))
cmp = lp->cl_lfn - rp->cl_lfn;
}
if (cmp < 0)
cmp = -1;
else if (cmp > 0)
cmp = 1;
return (cmp);
}
static int
by_ino_cmp(const void *vlp, const void *vrp)
{
const inode_dup_t *lp = (const inode_dup_t *)vlp;
const inode_dup_t *rp = (const inode_dup_t *)vrp;
int cmp;
cmp = lp->id_ino - rp->id_ino;
if (cmp < 0)
cmp = -1;
else if (cmp > 0)
cmp = 1;
return (cmp);
}
static int
by_lfn_cmp(const void *vlp, const void *vrp)
{
const reference_t *lp = (const reference_t *)vlp;
const reference_t *rp = (const reference_t *)vrp;
int cmp;
cmp = lp->ref_lfn - rp->ref_lfn;
if (cmp < 0)
cmp = -1;
else if (cmp > 0)
cmp = 1;
return (cmp);
}
static inode_dup_t *
new_inode_dup(fsck_ino_t inode)
{
inode_dup_t *new;
new = (inode_dup_t *)malloc(sizeof (inode_dup_t));
if (new == NULL)
errexit("Out of memory in new_inode_dup\n");
new->id_ino = inode;
avl_create(&new->id_fragments, by_lfn_cmp, sizeof (reference_t),
OFFSETOF(reference_t, ref_avl));
return (new);
}