/*
 *  pageinfo.c - /proc module to extract information on paging system
 */


/* Declare what kind of code we want from the header files */
#define __KERNEL__         /* We're part of the kernel */
#define MODULE             /* Not a permanent part, though. */

/* Standard headers */
#include <linux/module.h>       /* Specifically, a module */
#include <linux/kernel.h>       /* We're doing kernel work */
#include <linux/proc_fs.h>      /* Necessary because we use the proc fs */
#include <linux/version.h>
#include <linux/fs.h>
#include <linux/proc_fs.h>
#include <linux/mm.h>
#include <linux/mmzone.h>

/* Documentation macros */
MODULE_AUTHOR("Nicholas R. Howe)");
MODULE_DESCRIPTION("Paging Info Module");
MODULE_LICENSE("GPL");

static struct proc_dir_entry *Our_Proc_File;

/**************************************************************************/
/*                      /PROC ENTRY OPERATIONS                            */
/**************************************************************************/

static int pageinfo_proc_read(char *buf, char **start, off_t offset, 
			 int count, int *eof, void *data)
{
	int len = 0;
	unsigned long i;

	/* page information */
	struct page *page;
	unsigned long count_used=0, count_reserved=0;

	/* buddy system information */
	struct zone *zone;

	/* 
	 * print information about the pages in the system 
	 */
	
	for (i=0, page=mem_map; i<max_mapnr; i++, page++) {
		if (PageReserved(page))     /* reserved pages */
			count_reserved++;
		else if (page_count(page))  /* used pages */
			count_used++;
	}
		
	len += sprintf(buf+len, 
		       "page size     : %lu B\n"
		       "total pages   : %lu\n"
		       "reserved pages: %lu\n"
		       "used pages    : %lu\n"
		       "free memory   : %lu KB\n"
		       ,
		       PAGE_SIZE,
		       max_mapnr,
		       count_reserved,
		       count_used,
		       (unsigned long)((max_mapnr-count_reserved-count_used)*PAGE_SIZE)>>10);

	/*
	 * print information about the buddy system
	 */
 
	zone = &contig_page_data.node_zones[ZONE_NORMAL];

	len += sprintf(buf+len,
		       "\nZONE: %s\n"
		       "total pages  : %lu\n"
		       "free pages  : %lu\n"
		       "used pages  : %lu\n"
		       ,
		       zone->name,
		       zone->spanned_pages,
		       zone->free_pages,
		       zone->spanned_pages-zone->free_pages);

	/*************************************************************
	 * Add code here to print the number of areas in each order of
	 * the free area array (in "zone"). Also, print the number of
	 * pages in each order. The total number of pages should be
	 * the same indicated by the "zone->free_pages" variable
	 * above.
	 *************************************************************/

	*eof = 1;
	return(len);
} // pageinfo_proc_read()

static int __init procfs_init_module()
{
        int rv = 0;

        Our_Proc_File = create_proc_entry("pageinfo", 0644, NULL);
        
        if (Our_Proc_File == NULL) {
                rv = -ENOMEM;
                remove_proc_entry("pageinfo", &proc_root);
                printk(KERN_INFO "Error: Could not init /proc/pageinfo\n");
        }
        
        Our_Proc_File->read_proc = pageinfo_proc_read;
        Our_Proc_File->owner = THIS_MODULE;
        Our_Proc_File->mode = S_IFREG | S_IRUGO;
        Our_Proc_File->uid = 0;
        Our_Proc_File->gid = 0;
        Our_Proc_File->size = 37;

        printk(KERN_INFO "Created /proc/pageinfo.\n");      
        return rv;
}

static void __exit procfs_exit_module()
{
        remove_proc_entry("test", &proc_root);
        printk(KERN_INFO "/proc/test removed\n");
}

/* The macros below register the init and exit functions with the kernel */
module_init(procfs_init_module);
module_exit(procfs_exit_module);
