/*
 * Compressed rom filesystem for Linux.
 *
 * Copyright (C) 1999 Linus Torvalds.
 *
 * This file is released under the GPL.
 */

/*
 * These are the VFS interfaces to the compressed rom filesystem.
 * The actual compression is based on zlib, see the other files.
 */

#include <linux/module.h>
#include <linux/fs.h>
#include <linux/pagemap.h>
#include <linux/init.h>
#include <linux/string.h>
#include <linux/locks.h>

#include <asm/uaccess.h>
#define LINUX22
#ifdef LINUX22
#include <asm/pgtable.h>
#endif

#include "cramfs.h"

static struct super_operations cramfs_ops;
#ifdef LINUX22
static struct inode_operations cramfs_file_inode_operations;
static struct inode_operations cramfs_symlink_inode_operations;
#endif
static struct inode_operations cramfs_dir_inode_operations;
static struct file_operations cramfs_directory_operations;
#ifndef LINUX22
static struct address_space_operations cramfs_aops;
#endif

/* These two macros may change in future, to provide better st_ino
   semantics. */
#define CRAMINO(x)	((x)->offset?(x)->offset<<2:1)
#define OFFSET(x)	((x)->i_ino)

#ifdef LINUX22
static inline struct inode * new_inode(struct super_block *sb)
{
  struct inode *inode = get_empty_inode();
  if (inode) {
    inode->i_sb = sb;
    inode->i_dev = sb->s_dev;
  }
  return inode;
}
#endif

static struct inode *get_cramfs_inode(struct super_block *sb, struct cramfs_inode * cramfs_inode)
{
	struct inode * inode = new_inode(sb);

	if (inode) {
		inode->i_mode = cramfs_inode->mode;
		inode->i_uid = cramfs_inode->uid;
		inode->i_size = cramfs_inode->size;
		inode->i_gid = cramfs_inode->gid;
		inode->i_ino = CRAMINO(cramfs_inode);
		/* inode->i_nlink is left 1 - arguably wrong for directories,
		   but it's the best we can do without reading the directory
	           contents.  1 yields the right result in GNU find, even
		   without -noleaf option. */
		insert_inode_hash(inode);
		if (S_ISREG(inode->i_mode)) {
#ifdef LINUX22
			inode->i_op = &cramfs_file_inode_operations;
#else
			inode->i_fop = &generic_ro_fops;
			inode->i_data.a_ops = &cramfs_aops;
#endif
		} else if (S_ISDIR(inode->i_mode)) {
			inode->i_op = &cramfs_dir_inode_operations;
#ifndef LINUX22
			inode->i_fop = &cramfs_directory_operations;
#endif
		} else if (S_ISLNK(inode->i_mode)) {
#ifdef LINUX22
		        inode->i_op = &cramfs_symlink_inode_operations;
#else
			inode->i_op = &page_symlink_inode_operations;
			inode->i_data.a_ops = &cramfs_aops;
#endif
#ifndef LINUX22
			/* %%% */
		} else {
			inode->i_size = 0;
			init_special_inode(inode, inode->i_mode, cramfs_inode->size);
#endif
		}
	}
	return inode;
}

/*
 * We have our own block cache: don't fill up the buffer cache
 * with the rom-image, because the way the filesystem is set
 * up the accesses should be fairly regular and cached in the
 * page cache and dentry tree anyway..
 *
 * This also acts as a way to guarantee contiguous areas of up to
 * BLKS_PER_BUF*PAGE_CACHE_SIZE, so that the caller doesn't need to
 * worry about end-of-buffer issues even when decompressing a full
 * page cache.
 */
#define READ_BUFFERS (2)
/* NEXT_BUFFER(): Loop over [0..(READ_BUFFERS-1)]. */
#define NEXT_BUFFER(_ix) ((_ix) ^ 1)

/*
 * BLKS_PER_BUF_SHIFT should be at least 2 to allow for "compressed"
 * data that takes up more space than the original and with unlucky
 * alignment.
 */
#define BLKS_PER_BUF_SHIFT	(2)
#define BLKS_PER_BUF		(1 << BLKS_PER_BUF_SHIFT)
#define BUFFER_SIZE		(BLKS_PER_BUF*PAGE_CACHE_SIZE)

static unsigned char read_buffers[READ_BUFFERS][BUFFER_SIZE];
static unsigned buffer_blocknr[READ_BUFFERS];
static struct super_block * buffer_dev[READ_BUFFERS];
static int next_buffer;

/*
 * Returns a pointer to a buffer containing at least LEN bytes of
 * filesystem starting at byte offset OFFSET into the filesystem.
 */
static void *cramfs_read(struct super_block *sb, unsigned int offset, unsigned int len)
{
	struct buffer_head * bh_array[BLKS_PER_BUF];
	unsigned i, blocknr, buffer;
	char *data;

	if (!len)
		return NULL;
	blocknr = offset >> PAGE_CACHE_SHIFT;
	offset &= PAGE_CACHE_SIZE - 1;

	/* Check if an existing buffer already has the data.. */
	for (i = 0; i < READ_BUFFERS; i++) {
		unsigned int blk_offset;

		if (buffer_dev[i] != sb)
			continue;
		if (blocknr < buffer_blocknr[i])
			continue;
		blk_offset = (blocknr - buffer_blocknr[i]) << PAGE_CACHE_SHIFT;
		blk_offset += offset;
		if (blk_offset + len > BUFFER_SIZE)
			continue;
		return read_buffers[i] + blk_offset;
	}

	/* Ok, read in BLKS_PER_BUF pages completely first. */
	for (i = 0; i < BLKS_PER_BUF; i++)
		bh_array[i] = bread(sb->s_dev, blocknr + i, PAGE_CACHE_SIZE);

	/* Ok, copy them to the staging area without sleeping. */
	buffer = next_buffer;
	next_buffer = NEXT_BUFFER(buffer);
	buffer_blocknr[buffer] = blocknr;
	buffer_dev[buffer] = sb;

	data = read_buffers[buffer];
	for (i = 0; i < BLKS_PER_BUF; i++) {
		struct buffer_head * bh = bh_array[i];
		if (bh) {
			memcpy(data, bh->b_data, PAGE_CACHE_SIZE);
			brelse(bh);
		} else
			memset(data, 0, PAGE_CACHE_SIZE);
		data += PAGE_CACHE_SIZE;
	}
	return read_buffers[buffer] + offset;
}
			

static struct super_block * cramfs_read_super(struct super_block *sb, void *data, int silent)
{
	int i;
	struct cramfs_super super;
	unsigned long root_offset;
	struct super_block * retval = NULL;

	set_blocksize(sb->s_dev, PAGE_CACHE_SIZE);
	sb->s_blocksize = PAGE_CACHE_SIZE;
	sb->s_blocksize_bits = PAGE_CACHE_SHIFT;

	/* Invalidate the read buffers on mount: think disk change.. */
	for (i = 0; i < READ_BUFFERS; i++)
		buffer_blocknr[i] = -1;

	/* Read the first block and get the superblock from it */
	memcpy(&super, cramfs_read(sb, 0, sizeof(super)), sizeof(super));

	/* Do sanity checks on the superblock */
	if (super.magic != CRAMFS_MAGIC) {
		printk("wrong magic\n");
		goto out;
	}
	if (memcmp(super.signature, CRAMFS_SIGNATURE, sizeof(super.signature))) {
		printk("wrong signature\n");
		goto out;
	}
	if (super.flags & ~CRAMFS_SUPPORTED_FLAGS) {
		printk("unsupported filesystem features\n");
		goto out;
	}

	/* Check that the root inode is in a sane state */
	if (!S_ISDIR(super.root.mode)) {
		printk("root is not a directory\n");
		goto out;
	}
	root_offset = super.root.offset << 2;
	if (root_offset == 0)
		printk(KERN_INFO "cramfs: note: empty filesystem");
	else if (root_offset != sizeof(struct cramfs_super)) {
		printk("bad root offset %lu\n", root_offset);
		goto out;
	}

	/* Set it all up.. */
	sb->s_op	= &cramfs_ops;
	sb->s_root = d_alloc_root(get_cramfs_inode(sb, &super.root)
#ifdef LINUX22
				  , NULL
#endif
				  );
	retval = sb;

out:
	return retval;
}

#ifdef LINUX22
static int cramfs_statfs(struct super_block *sb, struct statfs *buf, int bufsize)
{
        struct statfs tmp;

        memset(&tmp, 0, sizeof(tmp));
	tmp.f_type = CRAMFS_MAGIC;
	tmp.f_bsize = PAGE_CACHE_SIZE;
	buf->f_namelen = 255;
        return copy_to_user(buf, &tmp, bufsize) ? -EFAULT : 0;
}
#else
static int cramfs_statfs(struct super_block *sb, struct statfs *buf)
{
	buf->f_type = CRAMFS_MAGIC;
	buf->f_bsize = PAGE_CACHE_SIZE;
	buf->f_bfree = 0;
	buf->f_bavail = 0;
	buf->f_ffree = 0;
	buf->f_namelen = 255;
	return 0;
}
#endif

/*
 * Read a cramfs directory entry.
 */
static int cramfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
{
	struct inode *inode = filp->f_dentry->d_inode;
	struct super_block *sb = inode->i_sb;
	unsigned int offset;
	int copied;

	/* Offset within the thing. */
	offset = filp->f_pos;
	if (offset >= inode->i_size)
		return 0;
	/* Directory entries are always 4-byte aligned */
	if (offset & 3)
		return -EINVAL;

	copied = 0;
	while (offset < inode->i_size) {
		struct cramfs_inode *de;
		unsigned long nextoffset;
		char *name;
		int namelen, error;

		de = cramfs_read(sb, OFFSET(inode) + offset, sizeof(*de)+256);
		name = (char *)(de+1);

		/*
		 * Namelengths on disk are shifted by two
		 * and the name padded out to 4-byte boundaries
		 * with zeroes.
		 */
		namelen = de->namelen << 2;
		nextoffset = offset + sizeof(*de) + namelen;
		for (;;) {
			if (!namelen)
				return -EIO;
			if (name[namelen-1])
				break;
			namelen--;
		}
		error = filldir(dirent, name, namelen, offset, CRAMINO(de)
#ifndef LINUX22
				,de->mode >> 12
#endif
				);
		if (error)
			break;

		offset = nextoffset;
		filp->f_pos = offset;
		copied++;
	}
	return 0;
}

/*
 * Lookup and fill in the inode data..
 */
static struct dentry * cramfs_lookup(struct inode *dir, struct dentry *dentry)
{
	unsigned int offset = 0;

	while (offset < dir->i_size) {
		struct cramfs_inode *de;
		char *name;
		int namelen;

		de = cramfs_read(dir->i_sb, OFFSET(dir) + offset, sizeof(*de)+256);
		name = (char *)(de+1);
		namelen = de->namelen << 2;
		offset += sizeof(*de) + namelen;

		/* Quick check that the name is roughly the right length */
		if (((dentry->d_name.len + 3) & ~3) != namelen)
			continue;

		for (;;) {
			if (!namelen)
				return ERR_PTR(-EIO);
			if (name[namelen-1])
				break;
			namelen--;
		}
		if (namelen != dentry->d_name.len)
			continue;
		if (memcmp(dentry->d_name.name, name, namelen))
			continue;
		d_add(dentry, get_cramfs_inode(dir->i_sb, de));
		return NULL;
	}
	d_add(dentry, NULL);
	return NULL;
}

static int cramfs_readpage(struct file *file, struct page * page)
{
#ifdef LINUX22
        struct dentry *dentry = file->f_dentry;
        struct inode *inode = dentry->d_inode;
#else
	struct inode *inode = (struct inode*)page->mapping->host;
#endif
	u32 maxblock, bytes_filled;

#ifdef LINUX22
        atomic_inc(&page->count);
        set_bit(PG_locked, &page->flags);
        clear_bit(PG_uptodate, &page->flags);
        clear_bit(PG_error, &page->flags);
#endif

	maxblock = (inode->i_size + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
	bytes_filled = 0;
#ifdef LINUX22
#define PAGE_INDEX(p) (p->offset >> PAGE_CACHE_SHIFT)
#else
#define PAGE_INDEX(p) (p->index)
#endif
	if (PAGE_INDEX(page) < maxblock) {
		struct super_block *sb = inode->i_sb;
		u32 blkptr_offset = OFFSET(inode) + PAGE_INDEX(page)*4;
		u32 start_offset, compr_len;

		start_offset = OFFSET(inode) + maxblock*4;
		if (PAGE_INDEX(page))
			start_offset = *(u32 *) cramfs_read(sb, blkptr_offset-4, 4);
		compr_len = (*(u32 *) cramfs_read(sb, blkptr_offset, 4)
			     - start_offset);
		if (compr_len == 0)
			; /* hole */
		else
			bytes_filled = cramfs_uncompress_block((void*)page_address(page),
				 PAGE_CACHE_SIZE,
				 cramfs_read(sb, start_offset, compr_len),
				 compr_len);
	}
	memset((void*)page_address(page) + bytes_filled, 0, PAGE_CACHE_SIZE - bytes_filled);
	flush_dcache_page(page);
#ifdef LINUX22
	set_bit(PG_uptodate, &page->flags);
        clear_bit(PG_locked, &page->flags);
        wake_up(&page->wait);
#else
	SetPageUptodate(page);
	UnlockPage(page);
#endif
	return 0;
}

#ifndef LINUX22
static struct address_space_operations cramfs_aops = {
  	readpage: cramfs_readpage,
};
#endif

/*
 * Our operations:
 */

#ifdef LINUX22
/*
 * This is a hack, and doesn't use the page cache at all.
 */
static int cramfs_readlink(struct dentry *dentry, char *buffer, int buflen)
{
  struct inode *inode = dentry->d_inode;
  u32 start_offset, compr_len;

  start_offset = OFFSET(inode) + 4;
  compr_len = (*(u32 *) cramfs_read(inode->i_sb, OFFSET(inode), 4)
	       - start_offset);

  return
    cramfs_uncompress_block((void*)buffer,
			    buflen,
			    cramfs_read(inode->i_sb, start_offset, compr_len),
			    compr_len);
}

static struct dentry *cramfs_follow_link(struct dentry *dentry,
					 struct dentry *base,
					 unsigned int follow)
{
  struct inode *inode = dentry->d_inode;
  char link[256];
  u32 start_offset, compr_len;

  start_offset = OFFSET(inode) + 4;
  compr_len = (*(u32 *) cramfs_read(inode->i_sb, OFFSET(inode), 4)
	       - start_offset);

  cramfs_uncompress_block((void*)link,
			  sizeof(link),
			  cramfs_read(inode->i_sb, start_offset, compr_len),
			  compr_len);
  link[inode->i_size] = '\0';

  return lookup_dentry(link, base, follow);
}

static struct file_operations generic_ro_fops = {
        read:           generic_file_read,
        mmap:           generic_file_mmap,
};

static struct inode_operations cramfs_file_inode_operations = {
	default_file_ops: &generic_ro_fops,
	readpage:         cramfs_readpage,
};

static struct inode_operations cramfs_symlink_inode_operations = {
  	readlink:	cramfs_readlink,
	follow_link:	cramfs_follow_link,
	readpage:       cramfs_readpage,
};
#endif

/*
 * A directory can only readdir
 */
static struct file_operations cramfs_directory_operations = {
#ifndef LINUX22
	read:		generic_read_dir,
#endif
	readdir:	cramfs_readdir,
};

static struct inode_operations cramfs_dir_inode_operations = {
#ifdef LINUX22
        default_file_ops: &cramfs_directory_operations,
#endif
	lookup:		cramfs_lookup,
};

static struct super_operations cramfs_ops = {
	statfs:		cramfs_statfs,
};

#ifdef LINUX22
static struct file_system_type cramfs_fs_type = {
  "cramfs",
  FS_REQUIRES_DEV,
  cramfs_read_super,
        NULL
};

__initfunc(int init_cramfs_fs(void))
{
	cramfs_uncompress_init();
	return register_filesystem(&cramfs_fs_type);
}

#ifdef MODULE

EXPORT_NO_SYMBOLS;

int
init_module(void)
{
  return init_cramfs_fs();
}

void
cleanup_module(void)
{
	cramfs_uncompress_exit();
	unregister_filesystem(&cramfs_fs_type);
}
#endif

#else
static DECLARE_FSTYPE_DEV(cramfs_fs_type, "cramfs", cramfs_read_super);

static int __init init_cramfs_fs(void)
{
	cramfs_uncompress_init();
	return register_filesystem(&cramfs_fs_type);
}

static void __exit exit_cramfs_fs(void)
{
	cramfs_uncompress_exit();
	unregister_filesystem(&cramfs_fs_type);
}

module_init(init_cramfs_fs)
module_exit(exit_cramfs_fs)
#endif
