cramfs: propagate uncompression errors
[safe/jmp/linux-2.6] / fs / cramfs / inode.c
index 350680f..46e5298 100644 (file)
@@ -23,7 +23,6 @@
 #include <linux/buffer_head.h>
 #include <linux/vfs.h>
 #include <linux/mutex.h>
-#include <asm/semaphore.h>
 
 #include <asm/uaccess.h>
 
@@ -44,58 +43,13 @@ static DEFINE_MUTEX(read_mutex);
 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)
 {
-       static struct timespec zerotime;
        struct cramfs_inode *cramfs_inode = opaque;
-       inode->i_mode = cramfs_inode->mode;
-       inode->i_uid = cramfs_inode->uid;
-       inode->i_size = cramfs_inode->size;
-       inode->i_blocks = (cramfs_inode->size - 1) / 512 + 1;
-       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
-          without -noleaf option. */
-       if (S_ISREG(inode->i_mode)) {
-               inode->i_fop = &generic_ro_fops;
-               inode->i_data.a_ops = &cramfs_aops;
-       } else if (S_ISDIR(inode->i_mode)) {
-               inode->i_op = &cramfs_dir_inode_operations;
-               inode->i_fop = &cramfs_directory_operations;
-       } else if (S_ISLNK(inode->i_mode)) {
-               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));
-       }
        return 0;
 }
 
@@ -105,12 +59,46 @@ static struct inode *get_cramfs_inode(struct super_block *sb,
        struct inode *inode = iget5_locked(sb, CRAMINO(cramfs_inode),
                                            cramfs_iget5_test, cramfs_iget5_set,
                                            cramfs_inode);
+       static struct timespec zerotime;
+
        if (inode && (inode->i_state & I_NEW)) {
+               inode->i_mode = cramfs_inode->mode;
+               inode->i_uid = cramfs_inode->uid;
+               inode->i_size = cramfs_inode->size;
+               inode->i_blocks = (cramfs_inode->size - 1) / 512 + 1;
+               inode->i_gid = cramfs_inode->gid;
+               /* Struct copy intentional */
+               inode->i_mtime = inode->i_atime = inode->i_ctime = zerotime;
+               /* 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. */
+               if (S_ISREG(inode->i_mode)) {
+                       inode->i_fop = &generic_ro_fops;
+                       inode->i_data.a_ops = &cramfs_aops;
+               } else if (S_ISDIR(inode->i_mode)) {
+                       inode->i_op = &cramfs_dir_inode_operations;
+                       inode->i_fop = &cramfs_directory_operations;
+               } else if (S_ISLNK(inode->i_mode)) {
+                       inode->i_op = &page_symlink_inode_operations;
+                       inode->i_data.a_ops = &cramfs_aops;
+               } else {
+                       init_special_inode(inode, inode->i_mode,
+                               old_decode_dev(cramfs_inode->size));
+               }
                unlock_new_inode(inode);
        }
        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
@@ -471,11 +459,14 @@ 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;
@@ -484,30 +475,43 @@ static int cramfs_readpage(struct file *file, struct page * page)
                start_offset = OFFSET(inode) + maxblock*4;
                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);
+                       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);
-               pgdata = kmap(page);
+
                if (compr_len == 0)
                        ; /* hole */
-               else if (compr_len > (PAGE_CACHE_SIZE << 1))
-                       printk(KERN_ERR "cramfs: bad compressed blocksize %u\n", compr_len);
-               else {
+               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);
                        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 const struct address_space_operations cramfs_aops = {
@@ -535,6 +539,7 @@ 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 int cramfs_get_sb(struct file_system_type *fs_type,