/*
 * Copyright (C) 2000-2002 ASANO Masahiro
 */

#include <unistd.h>
#include "crash.h"

PRIVATE addr_t operations();
const commandtable_t command_operations =
	{"operations", operations, "-a|-b|-d|-e|-f|-i|-s|-q|-v address", "print address_space, block_device, dentry, export, file, inode, super, dquot or vm operations"};

PRIVATE const char *address_space_op[] = {
	"ADDRESS SPACE OPERATIONS",
	"writepage", "readpage", "sync_page",
	"writepages", "vm_writeback", "set_page_dirty", "readpages",
	"prepare_write", "commit_write", "bmap", "invalidatepage",
	"releaspage", "direct_IO",
	NULL
};

PRIVATE const char *block_device_op[] = {
	"BLOCK DEVICE OPERATIONS",
	"open", "release", "ioctl", "check_media_change", "revalidate",
	"owner",
	NULL
};

PRIVATE const char *file_op[] = {
	"FILE OPERATIONS",
	"owner",
	"llseek", "read", "write", "readdir", "poll", "ioctl", "mmap",
	"open", "flush", "release", "fsync", "fasync", "lock",
	"readv", "writev",
	"sendpage", "get_unmapped_area",
	NULL
};

PRIVATE const char *inode_op[] = {
	"INODE OPERATIONS",
	"create", "lookup", "link", "unlink", "symlink",
	"mkdir", "rmdir", "mknod", "rename", "readlink", "follow_link",
	"truncate", "permission", "setattr", "getattr",
	"setxattr", "getxattr", "listxattr", "removexattr",
	NULL
};

PRIVATE const char *super_op[] = {
	"SUPER OPERATIONS",
	"alloc_inode", "destroy_inode",
	"read_inode", "dirty_inode", "write_inode", "put_inode", "drop_inode",
	"delete_inode", "put_super", "write_super", "write_super_lockfs",
	"unlockfs", "statfs", "remount_fs", "clear_inode", "umount_begin",
	"show_options",
	NULL
};

PRIVATE const char *dquot_op[] = {
	"DQUOT OPERATIONS",
	"initialize", "drop", "alloc_space", "alloc_inode", "free_space",
	"free_inode", "transfer",
	NULL
};

PRIVATE const char *export_op[] = {
	"EXPORT OPERATIONS",
	"decode_fh", "encode_fh", "get_name", "get_parent", "get_dentry",
	"find_exported_dentry",
	NULL
};

PRIVATE const char *dentry_op[] = {
	"DENTRY OPERATIONS",
	"revalidate", "hash", "compare", "delete", "release", "iput",
	NULL
};

PRIVATE const char *vm_op[] = {
	"VM OPERATIONS STRUCT",
	"open", "close", "nopage",
	NULL
};

const char head_operations[] = "%24s:   ADDRESS  SYMBOL\n";

void
print_operations(addr, op)
	addr_t addr;
	const char **op;
{
	addr_t buf[32];
	const char *p;
	int i;

	memread(addr, sizeof(buf), buf, "operations");
	mprintf(head_operations, op[0]);
	for (i = 1; op[i]; i++) {
		p = getsymstr(buf[i - 1]);
		mprintf("%24s:  " FPTR "  %s\n", op[i], buf[i - 1], p? p: "-");
	}
}

PRIVATE addr_t
operations()
{
	int i, c;
	const char **op = NULL;
	addr_t addr;

	while ((c = getopt(argcnt, args, "abdefiqsv")) != EOF) {
		switch (c) {
		case 'a':
			op = address_space_op;
			break;
		case 'b':
			op = block_device_op;
			break;
		case 'e':
			op = export_op;
			break;
		case 'f':
			op = file_op;
			break;
		case 'i':
			op = inode_op;
			break;
		case 's':
			op = super_op;
			break;
		case 'q':
			op = dquot_op;
			break;
		case 'd':
			op = dentry_op;
			break;
		case 'v':
			op = vm_op;
			break;
		default:
			THROW(usage);
		}
	}
	if (op == NULL || optind == argcnt) {
		THROW(usage);
	}

	for (i = optind; i < argcnt; i++) {
		addr = getaddr(args[i]);
		print_operations(addr, op);
	}
	return 0;
}
