ALSA: es1688: add pedantic range checks
[safe/jmp/linux-2.6] / fs / cramfs / inode.c
index 7fe8541..dd3634e 100644 (file)
 #include <linux/cramfs_fs_sb.h>
 #include <linux/buffer_head.h>
 #include <linux/vfs.h>
-#include <asm/semaphore.h>
+#include <linux/mutex.h>
 
 #include <asm/uaccess.h>
 
-static struct super_operations cramfs_ops;
-static struct inode_operations cramfs_dir_inode_operations;
-static struct file_operations cramfs_directory_operations;
-static struct address_space_operations cramfs_aops;
+static const struct super_operations cramfs_ops;
+static const struct inode_operations cramfs_dir_inode_operations;
+static const struct file_operations cramfs_directory_operations;
+static const struct address_space_operations cramfs_aops;
 
-static DECLARE_MUTEX(read_mutex);
+static DEFINE_MUTEX(read_mutex);
 
 
 /* These two macros may change in future, to provide better st_ino
    semantics. */
-#define CRAMINO(x)     ((x)->offset?(x)->offset<<2:1)
+#define CRAMINO(x)     (((x)->offset && (x)->size)?(x)->offset<<2:1)
 #define OFFSET(x)      ((x)->i_ino)
 
 
 static int cramfs_iget5_test(struct inode *inode, void *opaque)
 {
        struct cramfs_inode *cramfs_inode = opaque;
-
-       if (inode->i_ino != CRAMINO(cramfs_inode))
-               return 0; /* does not match */
-
-       if (inode->i_ino != 1)
-               return 1;
-
-       /* all empty directories, char, block, pipe, and sock, share inode #1 */
-
-       if ((inode->i_mode != cramfs_inode->mode) ||
-           (inode->i_gid != cramfs_inode->gid) ||
-           (inode->i_uid != cramfs_inode->uid))
-               return 0; /* does not match */
-
-       if ((S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) &&
-           (inode->i_rdev != old_decode_dev(cramfs_inode->size)))
-               return 0; /* does not match */
-
-       return 1; /* matches */
+       return inode->i_ino == CRAMINO(cramfs_inode) && inode->i_ino != 1;
 }
 
 static int cramfs_iget5_set(struct inode *inode, void *opaque)
@@ -84,14 +66,12 @@ static struct inode *get_cramfs_inode(struct super_block *sb,
                inode->i_uid = cramfs_inode->uid;
                inode->i_size = cramfs_inode->size;
                inode->i_blocks = (cramfs_inode->size - 1) / 512 + 1;
-               inode->i_blksize = PAGE_CACHE_SIZE;
                inode->i_gid = cramfs_inode->gid;
                /* Struct copy intentional */
                inode->i_mtime = inode->i_atime = inode->i_ctime = zerotime;
-               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
+                  contents.  1 yields the right result in GNU find, even
                   without -noleaf option. */
                if (S_ISREG(inode->i_mode)) {
                        inode->i_fop = &generic_ro_fops;
@@ -103,8 +83,6 @@ static struct inode *get_cramfs_inode(struct super_block *sb,
                        inode->i_op = &page_symlink_inode_operations;
                        inode->i_data.a_ops = &cramfs_aops;
                } else {
-                       inode->i_size = 0;
-                       inode->i_blocks = 0;
                        init_special_inode(inode, inode->i_mode,
                                old_decode_dev(cramfs_inode->size));
                }
@@ -113,6 +91,14 @@ static struct inode *get_cramfs_inode(struct super_block *sb,
        return inode;
 }
 
+static void cramfs_drop_inode(struct inode *inode)
+{
+       if (inode->i_ino == 1)
+               generic_delete_inode(inode);
+       else
+               generic_drop_inode(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
@@ -150,7 +136,7 @@ static void *cramfs_read(struct super_block *sb, unsigned int offset, unsigned i
 {
        struct address_space *mapping = sb->s_bdev->bd_inode->i_mapping;
        struct page *pages[BLKS_PER_BUF];
-       unsigned i, blocknr, buffer, unread;
+       unsigned i, blocknr, buffer;
        unsigned long devsize;
        char *data;
 
@@ -177,14 +163,12 @@ static void *cramfs_read(struct super_block *sb, unsigned int offset, unsigned i
        devsize = mapping->host->i_size >> PAGE_CACHE_SHIFT;
 
        /* Ok, read in BLKS_PER_BUF pages completely first. */
-       unread = 0;
        for (i = 0; i < BLKS_PER_BUF; i++) {
                struct page *page = NULL;
 
                if (blocknr + i < devsize) {
-                       page = read_cache_page(mapping, blocknr + i,
-                               (filler_t *)mapping->a_ops->readpage,
-                               NULL);
+                       page = read_mapping_page_async(mapping, blocknr + i,
+                                                                       NULL);
                        /* synchronous error? */
                        if (IS_ERR(page))
                                page = NULL;
@@ -245,29 +229,37 @@ static int cramfs_fill_super(struct super_block *sb, void *data, int silent)
 
        sb->s_flags |= MS_RDONLY;
 
-       sbi = kmalloc(sizeof(struct cramfs_sb_info), GFP_KERNEL);
+       sbi = kzalloc(sizeof(struct cramfs_sb_info), GFP_KERNEL);
        if (!sbi)
                return -ENOMEM;
        sb->s_fs_info = sbi;
-       memset(sbi, 0, sizeof(struct cramfs_sb_info));
 
        /* Invalidate the read buffers on mount: think disk change.. */
-       down(&read_mutex);
+       mutex_lock(&read_mutex);
        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));
-       up(&read_mutex);
+       mutex_unlock(&read_mutex);
 
        /* Do sanity checks on the superblock */
        if (super.magic != CRAMFS_MAGIC) {
+               /* check for wrong endianess */
+               if (super.magic == CRAMFS_MAGIC_WEND) {
+                       if (!silent)
+                               printk(KERN_ERR "cramfs: wrong endianess\n");
+                       goto out;
+               }
+
                /* check at 512 byte offset */
-               down(&read_mutex);
+               mutex_lock(&read_mutex);
                memcpy(&super, cramfs_read(sb, 512, sizeof(super)), sizeof(super));
-               up(&read_mutex);
+               mutex_unlock(&read_mutex);
                if (super.magic != CRAMFS_MAGIC) {
-                       if (!silent)
+                       if (super.magic == CRAMFS_MAGIC_WEND && !silent)
+                               printk(KERN_ERR "cramfs: wrong endianess\n");
+                       else if (!silent)
                                printk(KERN_ERR "cramfs: wrong magic\n");
                        goto out;
                }
@@ -323,8 +315,11 @@ out:
        return -EINVAL;
 }
 
-static int cramfs_statfs(struct super_block *sb, struct kstatfs *buf)
+static int cramfs_statfs(struct dentry *dentry, struct kstatfs *buf)
 {
+       struct super_block *sb = dentry->d_sb;
+       u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
+
        buf->f_type = CRAMFS_MAGIC;
        buf->f_bsize = PAGE_CACHE_SIZE;
        buf->f_blocks = CRAMFS_SB(sb)->blocks;
@@ -332,6 +327,8 @@ static int cramfs_statfs(struct super_block *sb, struct kstatfs *buf)
        buf->f_bavail = 0;
        buf->f_files = CRAMFS_SB(sb)->files;
        buf->f_ffree = 0;
+       buf->f_fsid.val[0] = (u32)id;
+       buf->f_fsid.val[1] = (u32)(id >> 32);
        buf->f_namelen = CRAMFS_MAXPATHLEN;
        return 0;
 }
@@ -341,7 +338,7 @@ static int cramfs_statfs(struct super_block *sb, struct kstatfs *buf)
  */
 static int cramfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
 {
-       struct inode *inode = filp->f_dentry->d_inode;
+       struct inode *inode = filp->f_path.dentry->d_inode;
        struct super_block *sb = inode->i_sb;
        char *buf;
        unsigned int offset;
@@ -355,7 +352,7 @@ static int cramfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
        if (offset & 3)
                return -EINVAL;
 
-       buf = kmalloc(256, GFP_KERNEL);
+       buf = kmalloc(CRAMFS_MAXPATHLEN, GFP_KERNEL);
        if (!buf)
                return -ENOMEM;
 
@@ -368,8 +365,8 @@ static int cramfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
                mode_t mode;
                int namelen, error;
 
-               down(&read_mutex);
-               de = cramfs_read(sb, OFFSET(inode) + offset, sizeof(*de)+256);
+               mutex_lock(&read_mutex);
+               de = cramfs_read(sb, OFFSET(inode) + offset, sizeof(*de)+CRAMFS_MAXPATHLEN);
                name = (char *)(de+1);
 
                /*
@@ -381,7 +378,7 @@ static int cramfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
                memcpy(buf, name, namelen);
                ino = CRAMINO(de);
                mode = de->mode;
-               up(&read_mutex);
+               mutex_unlock(&read_mutex);
                nextoffset = offset + sizeof(*de) + namelen;
                for (;;) {
                        if (!namelen) {
@@ -412,14 +409,14 @@ static struct dentry * cramfs_lookup(struct inode *dir, struct dentry *dentry, s
        unsigned int offset = 0;
        int sorted;
 
-       down(&read_mutex);
+       mutex_lock(&read_mutex);
        sorted = CRAMFS_SB(dir->i_sb)->flags & CRAMFS_FLAG_SORTED_DIRS;
        while (offset < dir->i_size) {
                struct cramfs_inode *de;
                char *name;
                int namelen, retval;
 
-               de = cramfs_read(dir->i_sb, OFFSET(dir) + offset, sizeof(*de)+256);
+               de = cramfs_read(dir->i_sb, OFFSET(dir) + offset, sizeof(*de)+CRAMFS_MAXPATHLEN);
                name = (char *)(de+1);
 
                /* Try to take advantage of sorted directories */
@@ -435,7 +432,7 @@ static struct dentry * cramfs_lookup(struct inode *dir, struct dentry *dentry, s
 
                for (;;) {
                        if (!namelen) {
-                               up(&read_mutex);
+                               mutex_unlock(&read_mutex);
                                return ERR_PTR(-EIO);
                        }
                        if (name[namelen-1])
@@ -449,7 +446,7 @@ static struct dentry * cramfs_lookup(struct inode *dir, struct dentry *dentry, s
                        continue;
                if (!retval) {
                        struct cramfs_inode entry = *de;
-                       up(&read_mutex);
+                       mutex_unlock(&read_mutex);
                        d_add(dentry, get_cramfs_inode(dir->i_sb, &entry));
                        return NULL;
                }
@@ -457,7 +454,7 @@ static struct dentry * cramfs_lookup(struct inode *dir, struct dentry *dentry, s
                if (sorted)
                        break;
        }
-       up(&read_mutex);
+       mutex_unlock(&read_mutex);
        d_add(dentry, NULL);
        return NULL;
 }
@@ -465,44 +462,62 @@ static struct dentry * cramfs_lookup(struct inode *dir, struct dentry *dentry, s
 static int cramfs_readpage(struct file *file, struct page * page)
 {
        struct inode *inode = page->mapping->host;
-       u32 maxblock, bytes_filled;
+       u32 maxblock;
+       int bytes_filled;
        void *pgdata;
 
        maxblock = (inode->i_size + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
        bytes_filled = 0;
+       pgdata = kmap(page);
+
        if (page->index < maxblock) {
                struct super_block *sb = inode->i_sb;
                u32 blkptr_offset = OFFSET(inode) + page->index*4;
                u32 start_offset, compr_len;
 
                start_offset = OFFSET(inode) + maxblock*4;
-               down(&read_mutex);
+               mutex_lock(&read_mutex);
                if (page->index)
-                       start_offset = *(u32 *) cramfs_read(sb, blkptr_offset-4, 4);
-               compr_len = (*(u32 *) cramfs_read(sb, blkptr_offset, 4) - start_offset);
-               up(&read_mutex);
-               pgdata = kmap(page);
+                       start_offset = *(u32 *) cramfs_read(sb, blkptr_offset-4,
+                               4);
+               compr_len = (*(u32 *) cramfs_read(sb, blkptr_offset, 4) -
+                       start_offset);
+               mutex_unlock(&read_mutex);
+
                if (compr_len == 0)
                        ; /* hole */
-               else {
-                       down(&read_mutex);
+               else if (unlikely(compr_len > (PAGE_CACHE_SIZE << 1))) {
+                       pr_err("cramfs: bad compressed blocksize %u\n",
+                               compr_len);
+                       goto err;
+               } else {
+                       mutex_lock(&read_mutex);
                        bytes_filled = cramfs_uncompress_block(pgdata,
                                 PAGE_CACHE_SIZE,
                                 cramfs_read(sb, start_offset, compr_len),
                                 compr_len);
-                       up(&read_mutex);
+                       mutex_unlock(&read_mutex);
+                       if (unlikely(bytes_filled < 0))
+                               goto err;
                }
-       } else
-               pgdata = kmap(page);
+       }
+
        memset(pgdata + bytes_filled, 0, PAGE_CACHE_SIZE - bytes_filled);
-       kunmap(page);
        flush_dcache_page(page);
+       kunmap(page);
        SetPageUptodate(page);
        unlock_page(page);
        return 0;
+
+err:
+       kunmap(page);
+       ClearPageUptodate(page);
+       SetPageError(page);
+       unlock_page(page);
+       return 0;
 }
 
-static struct address_space_operations cramfs_aops = {
+static const struct address_space_operations cramfs_aops = {
        .readpage = cramfs_readpage
 };
 
@@ -513,26 +528,28 @@ static struct address_space_operations cramfs_aops = {
 /*
  * A directory can only readdir
  */
-static struct file_operations cramfs_directory_operations = {
+static const struct file_operations cramfs_directory_operations = {
        .llseek         = generic_file_llseek,
        .read           = generic_read_dir,
        .readdir        = cramfs_readdir,
 };
 
-static struct inode_operations cramfs_dir_inode_operations = {
+static const struct inode_operations cramfs_dir_inode_operations = {
        .lookup         = cramfs_lookup,
 };
 
-static struct super_operations cramfs_ops = {
+static const struct super_operations cramfs_ops = {
        .put_super      = cramfs_put_super,
        .remount_fs     = cramfs_remount,
        .statfs         = cramfs_statfs,
+       .drop_inode     = cramfs_drop_inode,
 };
 
-static struct super_block *cramfs_get_sb(struct file_system_type *fs_type,
-       int flags, const char *dev_name, void *data)
+static int cramfs_get_sb(struct file_system_type *fs_type,
+       int flags, const char *dev_name, void *data, struct vfsmount *mnt)
 {
-       return get_sb_bdev(fs_type, flags, dev_name, data, cramfs_fill_super);
+       return get_sb_bdev(fs_type, flags, dev_name, data, cramfs_fill_super,
+                          mnt);
 }
 
 static struct file_system_type cramfs_fs_type = {
@@ -545,8 +562,15 @@ static struct file_system_type cramfs_fs_type = {
 
 static int __init init_cramfs_fs(void)
 {
-       cramfs_uncompress_init();
-       return register_filesystem(&cramfs_fs_type);
+       int rv;
+
+       rv = cramfs_uncompress_init();
+       if (rv < 0)
+               return rv;
+       rv = register_filesystem(&cramfs_fs_type);
+       if (rv < 0)
+               cramfs_uncompress_exit();
+       return rv;
 }
 
 static void __exit exit_cramfs_fs(void)