#!/bin/bash
# $Header: /cvsroot/lustre-tools/src/read-meta.sh,v 1.3 2011/03/16 18:43:41 jrappley Exp $

export PATH=/usr/local/bin:$PATH
LDEV_DIR=/usr/local/etc/lustre
dump_meta() {

    # Don't want to run this in the background. Use logsave? tee?

    for fs in $fslist; do
	ldev -c $LDEV_DIR/ldev-${fs}.conf \
	    "bash -c '/sbin/dumpe2fs %d > %f-ost%i.meta 2>/dev/null'"
    done
}

read_inodes() {
    for fs in $fslist; do
	echo -n "$fs "
	ldev -c $LDEV_DIR/ldev-${fs}.conf blockdev -v --setra 0 %d
	ldev -c $LDEV_DIR/ldev-${fs}.conf \
	    read-inodes -m %f-ost%i.meta -d %d
    done
    wait
}

scan_dirs() {
    for fs in $fslist; do
	echo -n "$fs "
	for i in {0..31}; do
	    echo -n "$i ";
	    ldev -c $LDEV_DIR/ldev-${fs}.conf \
		debugfs -c -R \"ls O/0/d$i\" %d > /dev/null;
	done
	echo
    done
    wait
}

progname=$(basename $0)

# Don't continue unless we're an OSS.

if [[ ! -d /proc/fs/lustre/obdfilter ]]; then
    echo "Not an OSS; exiting"
    exit 1
fi

if [[ -z $1 ]]; then
    echo "Usage: $progname [dump|read]"
    exit 1
fi

if [[ ! ($1 = "dump" || $1 = "read") ]]; then
    echo "Unknown option $1, exiting"
    exit 1
fi

echo "$progname starting at $(date)"

fslist=$(ls -d /proc/fs/lustre/obdfilter/*OST*|sed -e 's/\/proc\/fs\/lustre\/obdfilter\/\(.*\)-OST.*/\1/'|sort|uniq)

if [[ -z "$fslist" ]]; then
    echo "No OSTs mounted, exiting"
    exit 1
fi

case $1 in
    dump)
	echo "Dumping metadata layout"
	dump_meta
	;;
    read)
	echo "Reading metadata"
	read_inodes
	scan_dirs
	;;
    *)
	# Given the check above, we shouldn't get here!
	echo "Unknown option $1, exiting"
	exit 1
esac

echo "$progname done at $(date)"
