ext4: Set journal pointer to NULL when journal is released
[pandora-kernel.git] / fs / ext4 / super.c
index c81a8e7..1b330cd 100644 (file)
@@ -21,8 +21,6 @@
 #include <linux/fs.h>
 #include <linux/time.h>
 #include <linux/jbd2.h>
-#include <linux/ext4_fs.h>
-#include <linux/ext4_jbd2.h>
 #include <linux/slab.h>
 #include <linux/init.h>
 #include <linux/blkdev.h>
 #include <linux/seq_file.h>
 #include <linux/log2.h>
 #include <linux/crc16.h>
-
 #include <asm/uaccess.h>
 
+#include "ext4.h"
+#include "ext4_jbd2.h"
 #include "xattr.h"
 #include "acl.h"
 #include "namei.h"
@@ -135,7 +134,7 @@ handle_t *ext4_journal_start_sb(struct super_block *sb, int nblocks)
         * take the FS itself readonly cleanly. */
        journal = EXT4_SB(sb)->s_journal;
        if (is_journal_aborted(journal)) {
-               ext4_abort(sb, __FUNCTION__,
+               ext4_abort(sb, __func__,
                           "Detected aborted journal");
                return ERR_PTR(-EROFS);
        }
@@ -355,7 +354,7 @@ void ext4_update_dynamic_rev(struct super_block *sb)
        if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV)
                return;
 
-       ext4_warning(sb, __FUNCTION__,
+       ext4_warning(sb, __func__,
                     "updating to rev %d because of new feature flag, "
                     "running e2fsck is recommended",
                     EXT4_DYNAMIC_REV);
@@ -507,6 +506,7 @@ static void ext4_put_super (struct super_block * sb)
        ext4_ext_release(sb);
        ext4_xattr_put_super(sb);
        jbd2_journal_destroy(sbi->s_journal);
+       sbi->s_journal = NULL;
        if (!(sb->s_flags & MS_RDONLY)) {
                EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
                es->s_state = cpu_to_le16(sbi->s_mount_state);
@@ -518,6 +518,7 @@ static void ext4_put_super (struct super_block * sb)
        for (i = 0; i < sbi->s_gdb_count; i++)
                brelse(sbi->s_group_desc[i]);
        kfree(sbi->s_group_desc);
+       kfree(sbi->s_flex_groups);
        percpu_counter_destroy(&sbi->s_freeblocks_counter);
        percpu_counter_destroy(&sbi->s_freeinodes_counter);
        percpu_counter_destroy(&sbi->s_dirs_counter);
@@ -730,8 +731,15 @@ static int ext4_show_options(struct seq_file *seq, struct vfsmount *vfs)
                seq_printf(seq, ",commit=%u",
                           (unsigned) (sbi->s_commit_interval / HZ));
        }
-       if (test_opt(sb, BARRIER))
-               seq_puts(seq, ",barrier=1");
+       /*
+        * We're changing the default of barrier mount option, so
+        * let's always display its mount state so it's clear what its
+        * status is.
+        */
+       seq_puts(seq, ",barrier=");
+       seq_puts(seq, test_opt(sb, BARRIER) ? "1" : "0");
+       if (test_opt(sb, JOURNAL_ASYNC_COMMIT))
+               seq_puts(seq, ",journal_async_commit");
        if (test_opt(sb, NOBH))
                seq_puts(seq, ",nobh");
        if (!test_opt(sb, EXTENTS))
@@ -945,8 +953,8 @@ static match_table_t tokens = {
        {Opt_mballoc, "mballoc"},
        {Opt_nomballoc, "nomballoc"},
        {Opt_stripe, "stripe=%u"},
-       {Opt_err, NULL},
        {Opt_resize, "resize"},
+       {Opt_err, NULL},
 };
 
 static ext4_fsblk_t get_sb_block(void **data)
@@ -980,7 +988,7 @@ static int parse_options (char *options, struct super_block *sb,
        int data_opt = 0;
        int option;
 #ifdef CONFIG_QUOTA
-       int qtype;
+       int qtype, qfmt;
        char *qname;
 #endif
 
@@ -1163,9 +1171,11 @@ static int parse_options (char *options, struct super_block *sb,
                case Opt_grpjquota:
                        qtype = GRPQUOTA;
 set_qf_name:
-                       if (sb_any_quota_enabled(sb)) {
+                       if ((sb_any_quota_enabled(sb) ||
+                            sb_any_quota_suspended(sb)) &&
+                           !sbi->s_qf_names[qtype]) {
                                printk(KERN_ERR
-                                       "EXT4-fs: Cannot change journalled "
+                                       "EXT4-fs: Cannot change journaled "
                                        "quota options when quota turned on.\n");
                                return 0;
                        }
@@ -1201,9 +1211,11 @@ set_qf_name:
                case Opt_offgrpjquota:
                        qtype = GRPQUOTA;
 clear_qf_name:
-                       if (sb_any_quota_enabled(sb)) {
+                       if ((sb_any_quota_enabled(sb) ||
+                            sb_any_quota_suspended(sb)) &&
+                           sbi->s_qf_names[qtype]) {
                                printk(KERN_ERR "EXT4-fs: Cannot change "
-                                       "journalled quota options when "
+                                       "journaled quota options when "
                                        "quota turned on.\n");
                                return 0;
                        }
@@ -1214,10 +1226,20 @@ clear_qf_name:
                        sbi->s_qf_names[qtype] = NULL;
                        break;
                case Opt_jqfmt_vfsold:
-                       sbi->s_jquota_fmt = QFMT_VFS_OLD;
-                       break;
+                       qfmt = QFMT_VFS_OLD;
+                       goto set_qf_format;
                case Opt_jqfmt_vfsv0:
-                       sbi->s_jquota_fmt = QFMT_VFS_V0;
+                       qfmt = QFMT_VFS_V0;
+set_qf_format:
+                       if ((sb_any_quota_enabled(sb) ||
+                            sb_any_quota_suspended(sb)) &&
+                           sbi->s_jquota_fmt != qfmt) {
+                               printk(KERN_ERR "EXT4-fs: Cannot change "
+                                       "journaled quota options when "
+                                       "quota turned on.\n");
+                               return 0;
+                       }
+                       sbi->s_jquota_fmt = qfmt;
                        break;
                case Opt_quota:
                case Opt_usrquota:
@@ -1242,6 +1264,9 @@ clear_qf_name:
                case Opt_quota:
                case Opt_usrquota:
                case Opt_grpquota:
+                       printk(KERN_ERR
+                               "EXT4-fs: quota options not supported.\n");
+                       break;
                case Opt_usrjquota:
                case Opt_grpjquota:
                case Opt_offusrjquota:
@@ -1249,7 +1274,7 @@ clear_qf_name:
                case Opt_jqfmt_vfsold:
                case Opt_jqfmt_vfsv0:
                        printk(KERN_ERR
-                               "EXT4-fs: journalled quota options not "
+                               "EXT4-fs: journaled quota options not "
                                "supported.\n");
                        break;
                case Opt_noquota:
@@ -1334,14 +1359,14 @@ clear_qf_name:
                }
 
                if (!sbi->s_jquota_fmt) {
-                       printk(KERN_ERR "EXT4-fs: journalled quota format "
+                       printk(KERN_ERR "EXT4-fs: journaled quota format "
                                        "not specified.\n");
                        return 0;
                }
        } else {
                if (sbi->s_jquota_fmt) {
-                       printk(KERN_ERR "EXT4-fs: journalled quota format "
-                                       "specified with no journalling "
+                       printk(KERN_ERR "EXT4-fs: journaled quota format "
+                                       "specified with no journaling "
                                        "enabled.\n");
                        return 0;
                }
@@ -1388,11 +1413,11 @@ static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
                 * a plain journaled filesystem we can keep it set as
                 * valid forever! :)
                 */
-       es->s_state = cpu_to_le16(le16_to_cpu(es->s_state) & ~EXT4_VALID_FS);
+       es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
 #endif
        if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
                es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
-       es->s_mnt_count=cpu_to_le16(le16_to_cpu(es->s_mnt_count) + 1);
+       le16_add_cpu(&es->s_mnt_count, 1);
        es->s_mtime = cpu_to_le32(get_seconds());
        ext4_update_dynamic_rev(sb);
        EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
@@ -1419,6 +1444,54 @@ static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
        return res;
 }
 
+static int ext4_fill_flex_info(struct super_block *sb)
+{
+       struct ext4_sb_info *sbi = EXT4_SB(sb);
+       struct ext4_group_desc *gdp = NULL;
+       struct buffer_head *bh;
+       ext4_group_t flex_group_count;
+       ext4_group_t flex_group;
+       int groups_per_flex = 0;
+       __u64 block_bitmap = 0;
+       int i;
+
+       if (!sbi->s_es->s_log_groups_per_flex) {
+               sbi->s_log_groups_per_flex = 0;
+               return 1;
+       }
+
+       sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex;
+       groups_per_flex = 1 << sbi->s_log_groups_per_flex;
+
+       flex_group_count = (sbi->s_groups_count + groups_per_flex - 1) /
+               groups_per_flex;
+       sbi->s_flex_groups = kmalloc(flex_group_count *
+                                    sizeof(struct flex_groups), GFP_KERNEL);
+       if (sbi->s_flex_groups == NULL) {
+               printk(KERN_ERR "EXT4-fs: not enough memory\n");
+               goto failed;
+       }
+       memset(sbi->s_flex_groups, 0, flex_group_count *
+              sizeof(struct flex_groups));
+
+       gdp = ext4_get_group_desc(sb, 1, &bh);
+       block_bitmap = ext4_block_bitmap(sb, gdp) - 1;
+
+       for (i = 0; i < sbi->s_groups_count; i++) {
+               gdp = ext4_get_group_desc(sb, i, &bh);
+
+               flex_group = ext4_flex_group(sbi, i);
+               sbi->s_flex_groups[flex_group].free_inodes +=
+                       le16_to_cpu(gdp->bg_free_inodes_count);
+               sbi->s_flex_groups[flex_group].free_blocks +=
+                       le16_to_cpu(gdp->bg_free_blocks_count);
+       }
+
+       return 1;
+failed:
+       return 0;
+}
+
 __le16 ext4_group_desc_csum(struct ext4_sb_info *sbi, __u32 block_group,
                            struct ext4_group_desc *gdp)
 {
@@ -1485,36 +1558,33 @@ static int ext4_check_descriptors(struct super_block *sb)
                block_bitmap = ext4_block_bitmap(sb, gdp);
                if (block_bitmap < first_block || block_bitmap > last_block)
                {
-                       ext4_error (sb, "ext4_check_descriptors",
-                                   "Block bitmap for group %lu"
-                                   " not in group (block %llu)!",
-                                   i, block_bitmap);
+                       printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
+                              "Block bitmap for group %lu not in group "
+                              "(block %llu)!", i, block_bitmap);
                        return 0;
                }
                inode_bitmap = ext4_inode_bitmap(sb, gdp);
                if (inode_bitmap < first_block || inode_bitmap > last_block)
                {
-                       ext4_error (sb, "ext4_check_descriptors",
-                                   "Inode bitmap for group %lu"
-                                   " not in group (block %llu)!",
-                                   i, inode_bitmap);
+                       printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
+                              "Inode bitmap for group %lu not in group "
+                              "(block %llu)!", i, inode_bitmap);
                        return 0;
                }
                inode_table = ext4_inode_table(sb, gdp);
                if (inode_table < first_block ||
                    inode_table + sbi->s_itb_per_group - 1 > last_block)
                {
-                       ext4_error (sb, "ext4_check_descriptors",
-                                   "Inode table for group %lu"
-                                   " not in group (block %llu)!",
-                                   i, inode_table);
+                       printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
+                              "Inode table for group %lu not in group "
+                              "(block %llu)!", i, inode_table);
                        return 0;
                }
                if (!ext4_group_desc_csum_verify(sbi, i, gdp)) {
-                       ext4_error(sb, __FUNCTION__,
-                                  "Checksum for group %lu failed (%u!=%u)\n",
-                                   i, le16_to_cpu(ext4_group_desc_csum(sbi, i,
-                                   gdp)), le16_to_cpu(gdp->bg_checksum));
+                       printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
+                              "Checksum for group %lu failed (%u!=%u)\n",
+                              i, le16_to_cpu(ext4_group_desc_csum(sbi, i,
+                              gdp)), le16_to_cpu(gdp->bg_checksum));
                        return 0;
                }
                if (!flexbg_flag)
@@ -1585,7 +1655,7 @@ static void ext4_orphan_cleanup (struct super_block * sb,
                        int ret = ext4_quota_on_mount(sb, i);
                        if (ret < 0)
                                printk(KERN_ERR
-                                       "EXT4-fs: Cannot turn on journalled "
+                                       "EXT4-fs: Cannot turn on journaled "
                                        "quota: error %d\n", ret);
                }
        }
@@ -1594,8 +1664,8 @@ static void ext4_orphan_cleanup (struct super_block * sb,
        while (es->s_last_orphan) {
                struct inode *inode;
 
-               if (!(inode =
-                     ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan)))) {
+               inode = ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan));
+               if (IS_ERR(inode)) {
                        es->s_last_orphan = 0;
                        break;
                }
@@ -1605,7 +1675,7 @@ static void ext4_orphan_cleanup (struct super_block * sb,
                if (inode->i_nlink) {
                        printk(KERN_DEBUG
                                "%s: truncating inode %lu to %Ld bytes\n",
-                               __FUNCTION__, inode->i_ino, inode->i_size);
+                               __func__, inode->i_ino, inode->i_size);
                        jbd_debug(2, "truncating inode %lu to %Ld bytes\n",
                                  inode->i_ino, inode->i_size);
                        ext4_truncate(inode);
@@ -1613,7 +1683,7 @@ static void ext4_orphan_cleanup (struct super_block * sb,
                } else {
                        printk(KERN_DEBUG
                                "%s: deleting unreferenced inode %lu\n",
-                               __FUNCTION__, inode->i_ino);
+                               __func__, inode->i_ino);
                        jbd_debug(2, "deleting unreferenced inode %lu\n",
                                  inode->i_ino);
                        nr_orphans++;
@@ -1789,8 +1859,8 @@ static unsigned long ext4_get_stripe_size(struct ext4_sb_info *sbi)
 }
 
 static int ext4_fill_super (struct super_block *sb, void *data, int silent)
-                               __releases(kernel_sem)
-                               __acquires(kernel_sem)
+                               __releases(kernel_lock)
+                               __acquires(kernel_lock)
 
 {
        struct buffer_head * bh;
@@ -1830,11 +1900,6 @@ static int ext4_fill_super (struct super_block *sb, void *data, int silent)
                goto out_fail;
        }
 
-       if (!sb_set_blocksize(sb, blocksize)) {
-               printk(KERN_ERR "EXT4-fs: bad blocksize %d.\n", blocksize);
-               goto out_fail;
-       }
-
        /*
         * The ext4 superblock will not be buffer aligned for other than 1kB
         * block sizes.  We need to calculate the offset from buffer start.
@@ -1894,6 +1959,7 @@ static int ext4_fill_super (struct super_block *sb, void *data, int silent)
        sbi->s_resgid = le16_to_cpu(es->s_def_resgid);
 
        set_opt(sbi->s_mount_opt, RESERVATION);
+       set_opt(sbi->s_mount_opt, BARRIER);
 
        /*
         * turn on extents feature by default in ext4 filesystem
@@ -2116,6 +2182,14 @@ static int ext4_fill_super (struct super_block *sb, void *data, int silent)
                printk(KERN_ERR "EXT4-fs: group descriptors corrupted!\n");
                goto failed_mount2;
        }
+       if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
+               if (!ext4_fill_flex_info(sb)) {
+                       printk(KERN_ERR
+                              "EXT4-fs: unable to initialize "
+                              "flex_bg meta info!\n");
+                       goto failed_mount2;
+               }
+
        sbi->s_gdb_count = db_count;
        get_random_bytes(&sbi->s_next_generation, sizeof(u32));
        spin_lock_init(&sbi->s_next_gen_lock);
@@ -2176,6 +2250,29 @@ static int ext4_fill_super (struct super_block *sb, void *data, int silent)
            EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
                if (ext4_load_journal(sb, es, journal_devnum))
                        goto failed_mount3;
+               if (!(sb->s_flags & MS_RDONLY) &&
+                   EXT4_SB(sb)->s_journal->j_failed_commit) {
+                       printk(KERN_CRIT "EXT4-fs error (device %s): "
+                              "ext4_fill_super: Journal transaction "
+                              "%u is corrupt\n", sb->s_id, 
+                              EXT4_SB(sb)->s_journal->j_failed_commit);
+                       if (test_opt (sb, ERRORS_RO)) {
+                               printk (KERN_CRIT
+                                       "Mounting filesystem read-only\n");
+                               sb->s_flags |= MS_RDONLY;
+                               EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
+                               es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
+                       }
+                       if (test_opt(sb, ERRORS_PANIC)) {
+                               EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
+                               es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
+                               ext4_commit_super(sb, es, 1);
+                               printk(KERN_CRIT
+                                      "EXT4-fs (device %s): mount failed\n",
+                                     sb->s_id);
+                               goto failed_mount4;
+                       }
+               }
        } else if (journal_inum) {
                if (ext4_create_journal(sb, es, journal_inum))
                        goto failed_mount3;
@@ -2327,6 +2424,7 @@ cantfind_ext4:
 
 failed_mount4:
        jbd2_journal_destroy(sbi->s_journal);
+       sbi->s_journal = NULL;
 failed_mount3:
        percpu_counter_destroy(&sbi->s_freeblocks_counter);
        percpu_counter_destroy(&sbi->s_freeinodes_counter);
@@ -2699,9 +2797,9 @@ static void ext4_clear_journal_err(struct super_block * sb,
                char nbuf[16];
 
                errstr = ext4_decode_error(sb, j_errno, nbuf);
-               ext4_warning(sb, __FUNCTION__, "Filesystem error recorded "
+               ext4_warning(sb, __func__, "Filesystem error recorded "
                             "from previous mount: %s", errstr);
-               ext4_warning(sb, __FUNCTION__, "Marking fs in need of "
+               ext4_warning(sb, __func__, "Marking fs in need of "
                             "filesystem check.");
 
                EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
@@ -2828,7 +2926,7 @@ static int ext4_remount (struct super_block * sb, int * flags, char * data)
        }
 
        if (sbi->s_mount_opt & EXT4_MOUNT_ABORT)
-               ext4_abort(sb, __FUNCTION__, "Abort forced by user");
+               ext4_abort(sb, __func__, "Abort forced by user");
 
        sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
                ((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
@@ -3040,8 +3138,14 @@ static int ext4_dquot_drop(struct inode *inode)
 
        /* We may delete quota structure so we need to reserve enough blocks */
        handle = ext4_journal_start(inode, 2*EXT4_QUOTA_DEL_BLOCKS(inode->i_sb));
-       if (IS_ERR(handle))
+       if (IS_ERR(handle)) {
+               /*
+                * We call dquot_drop() anyway to at least release references
+                * to quota structures so that umount does not hang.
+                */
+               dquot_drop(inode);
                return PTR_ERR(handle);
+       }
        ret = dquot_drop(inode);
        err = ext4_journal_stop(handle);
        if (!ret)
@@ -3104,7 +3208,7 @@ static int ext4_release_dquot(struct dquot *dquot)
 
 static int ext4_mark_dquot_dirty(struct dquot *dquot)
 {
-       /* Are we journalling quotas? */
+       /* Are we journaling quotas? */
        if (EXT4_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
            EXT4_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
                dquot_mark_dquot_dirty(dquot);
@@ -3151,23 +3255,42 @@ static int ext4_quota_on(struct super_block *sb, int type, int format_id,
 
        if (!test_opt(sb, QUOTA))
                return -EINVAL;
-       /* Not journalling quota? */
-       if ((!EXT4_SB(sb)->s_qf_names[USRQUOTA] &&
-           !EXT4_SB(sb)->s_qf_names[GRPQUOTA]) || remount)
+       /* When remounting, no checks are needed and in fact, path is NULL */
+       if (remount)
                return vfs_quota_on(sb, type, format_id, path, remount);
+
        err = path_lookup(path, LOOKUP_FOLLOW, &nd);
        if (err)
                return err;
+
        /* Quotafile not on the same filesystem? */
        if (nd.path.mnt->mnt_sb != sb) {
                path_put(&nd.path);
                return -EXDEV;
        }
-       /* Quotafile not of fs root? */
-       if (nd.path.dentry->d_parent->d_inode != sb->s_root->d_inode)
-               printk(KERN_WARNING
-                       "EXT4-fs: Quota file not on filesystem root. "
-                       "Journalled quota will not work.\n");
+       /* Journaling quota? */
+       if (EXT4_SB(sb)->s_qf_names[type]) {
+               /* Quotafile not of fs root? */
+               if (nd.path.dentry->d_parent->d_inode != sb->s_root->d_inode)
+                       printk(KERN_WARNING
+                               "EXT4-fs: Quota file not on filesystem root. "
+                               "Journaled quota will not work.\n");
+       }
+
+       /*
+        * When we journal data on quota file, we have to flush journal to see
+        * all updates to the file when we bypass pagecache...
+        */
+       if (ext4_should_journal_data(nd.path.dentry->d_inode)) {
+               /*
+                * We don't need to lock updates but journal_flush() could
+                * otherwise be livelocked...
+                */
+               jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
+               jbd2_journal_flush(EXT4_SB(sb)->s_journal);
+               jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
+       }
+
        path_put(&nd.path);
        return vfs_quota_on(sb, type, format_id, path, remount);
 }
@@ -3267,8 +3390,10 @@ static ssize_t ext4_quota_write(struct super_block *sb, int type,
                blk++;
        }
 out:
-       if (len == towrite)
+       if (len == towrite) {
+               mutex_unlock(&inode->i_mutex);
                return err;
+       }
        if (inode->i_size < off+len-towrite) {
                i_size_write(inode, off+len-towrite);
                EXT4_I(inode)->i_disksize = inode->i_size;