Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/gerg/m68knommu
[safe/jmp/linux-2.6] / fs / nilfs2 / super.c
index f526169..03b34b7 100644 (file)
@@ -67,6 +67,11 @@ MODULE_DESCRIPTION("A New Implementation of the Log-structured Filesystem "
                   "(NILFS)");
 MODULE_LICENSE("GPL");
 
+struct kmem_cache *nilfs_inode_cachep;
+struct kmem_cache *nilfs_transaction_cachep;
+struct kmem_cache *nilfs_segbuf_cachep;
+struct kmem_cache *nilfs_btree_path_cache;
+
 static int nilfs_remount(struct super_block *sb, int *flags, char *data);
 
 /**
@@ -96,9 +101,6 @@ void nilfs_error(struct super_block *sb, const char *function,
        if (!(sb->s_flags & MS_RDONLY)) {
                struct the_nilfs *nilfs = sbi->s_nilfs;
 
-               if (!nilfs_test_opt(sbi, ERRORS_CONT))
-                       nilfs_detach_segment_constructor(sbi);
-
                down_write(&nilfs->ns_sem);
                if (!(nilfs->ns_mount_state & NILFS_ERROR_FS)) {
                        nilfs->ns_mount_state |= NILFS_ERROR_FS;
@@ -132,7 +134,6 @@ void nilfs_warning(struct super_block *sb, const char *function,
        va_end(args);
 }
 
-static struct kmem_cache *nilfs_inode_cachep;
 
 struct inode *nilfs_alloc_inode_common(struct the_nilfs *nilfs)
 {
@@ -158,34 +159,6 @@ void nilfs_destroy_inode(struct inode *inode)
        kmem_cache_free(nilfs_inode_cachep, NILFS_I(inode));
 }
 
-static void init_once(void *obj)
-{
-       struct nilfs_inode_info *ii = obj;
-
-       INIT_LIST_HEAD(&ii->i_dirty);
-#ifdef CONFIG_NILFS_XATTR
-       init_rwsem(&ii->xattr_sem);
-#endif
-       nilfs_btnode_cache_init_once(&ii->i_btnode_cache);
-       ii->i_bmap = (struct nilfs_bmap *)&ii->i_bmap_union;
-       inode_init_once(&ii->vfs_inode);
-}
-
-static int nilfs_init_inode_cache(void)
-{
-       nilfs_inode_cachep = kmem_cache_create("nilfs2_inode_cache",
-                                              sizeof(struct nilfs_inode_info),
-                                              0, SLAB_RECLAIM_ACCOUNT,
-                                              init_once);
-
-       return (nilfs_inode_cachep == NULL) ? -ENOMEM : 0;
-}
-
-static inline void nilfs_destroy_inode_cache(void)
-{
-       kmem_cache_destroy(nilfs_inode_cachep);
-}
-
 static void nilfs_clear_inode(struct inode *inode)
 {
        struct nilfs_inode_info *ii = NILFS_I(inode);
@@ -269,8 +242,8 @@ int nilfs_commit_super(struct nilfs_sb_info *sbi, int dupsb)
        int err;
 
        /* nilfs->sem must be locked by the caller. */
-       if (sbp[0]->s_magic != NILFS_SUPER_MAGIC) {
-               if (sbp[1] && sbp[1]->s_magic == NILFS_SUPER_MAGIC)
+       if (sbp[0]->s_magic != cpu_to_le16(NILFS_SUPER_MAGIC)) {
+               if (sbp[1] && sbp[1]->s_magic == cpu_to_le16(NILFS_SUPER_MAGIC))
                        nilfs_swap_super_block(nilfs);
                else {
                        printk(KERN_CRIT "NILFS: superblock broke on dev %s\n",
@@ -301,7 +274,7 @@ int nilfs_commit_super(struct nilfs_sb_info *sbi, int dupsb)
                memcpy(sbp[1], sbp[0], nilfs->ns_sbsize);
                nilfs->ns_sbwtime[1] = t;
        }
-       sbi->s_super->s_dirt = 0;
+       clear_nilfs_sb_dirty(nilfs);
        return nilfs_sync_super(sbi, dupsb);
 }
 
@@ -345,7 +318,7 @@ static int nilfs_sync_fs(struct super_block *sb, int wait)
                err = nilfs_construct_segment(sb);
 
        down_write(&nilfs->ns_sem);
-       if (sb->s_dirt)
+       if (nilfs_sb_dirty(nilfs))
                nilfs_commit_super(sbi, 1);
        up_write(&nilfs->ns_sem);
 
@@ -414,22 +387,6 @@ void nilfs_detach_checkpoint(struct nilfs_sb_info *sbi)
        up_write(&nilfs->ns_super_sem);
 }
 
-static int nilfs_mark_recovery_complete(struct nilfs_sb_info *sbi)
-{
-       struct the_nilfs *nilfs = sbi->s_nilfs;
-       int err = 0;
-
-       down_write(&nilfs->ns_sem);
-       if (!(nilfs->ns_mount_state & NILFS_VALID_FS)) {
-               nilfs->ns_mount_state |= NILFS_VALID_FS;
-               err = nilfs_commit_super(sbi, 1);
-               if (likely(!err))
-                       printk(KERN_INFO "NILFS: recovery complete.\n");
-       }
-       up_write(&nilfs->ns_sem);
-       return err;
-}
-
 static int nilfs_statfs(struct dentry *dentry, struct kstatfs *buf)
 {
        struct super_block *sb = dentry->d_sb;
@@ -455,7 +412,7 @@ static int nilfs_statfs(struct dentry *dentry, struct kstatfs *buf)
        /*
         * Compute the overhead
         *
-        * When distributing meta data blocks outside semgent structure,
+        * When distributing meta data blocks outside segment structure,
         * We must count them as the overhead.
         */
        overhead = 0;
@@ -489,12 +446,16 @@ static int nilfs_show_options(struct seq_file *seq, struct vfsmount *vfs)
        if (nilfs_test_opt(sbi, SNAPSHOT))
                seq_printf(seq, ",cp=%llu",
                           (unsigned long long int)sbi->s_snapshot_cno);
-       if (nilfs_test_opt(sbi, ERRORS_RO))
-               seq_printf(seq, ",errors=remount-ro");
        if (nilfs_test_opt(sbi, ERRORS_PANIC))
                seq_printf(seq, ",errors=panic");
+       if (nilfs_test_opt(sbi, ERRORS_CONT))
+               seq_printf(seq, ",errors=continue");
        if (nilfs_test_opt(sbi, STRICT_ORDER))
                seq_printf(seq, ",order=strict");
+       if (nilfs_test_opt(sbi, NORECOVERY))
+               seq_printf(seq, ",norecovery");
+       if (nilfs_test_opt(sbi, DISCARD))
+               seq_printf(seq, ",discard");
 
        return 0;
 }
@@ -563,8 +524,8 @@ static const struct export_operations nilfs_export_ops = {
 
 enum {
        Opt_err_cont, Opt_err_panic, Opt_err_ro,
-       Opt_nobarrier, Opt_snapshot, Opt_order,
-       Opt_err,
+       Opt_nobarrier, Opt_snapshot, Opt_order, Opt_norecovery,
+       Opt_discard, Opt_err,
 };
 
 static match_table_t tokens = {
@@ -574,6 +535,8 @@ static match_table_t tokens = {
        {Opt_nobarrier, "nobarrier"},
        {Opt_snapshot, "cp=%u"},
        {Opt_order, "order=%s"},
+       {Opt_norecovery, "norecovery"},
+       {Opt_discard, "discard"},
        {Opt_err, NULL}
 };
 
@@ -624,6 +587,12 @@ static int parse_options(char *options, struct super_block *sb)
                        sbi->s_snapshot_cno = option;
                        nilfs_set_opt(sbi, SNAPSHOT);
                        break;
+               case Opt_norecovery:
+                       nilfs_set_opt(sbi, NORECOVERY);
+                       break;
+               case Opt_discard:
+                       nilfs_set_opt(sbi, DISCARD);
+                       break;
                default:
                        printk(KERN_ERR
                               "NILFS: Unrecognized mount option \"%s\"\n", p);
@@ -638,7 +607,7 @@ nilfs_set_default_options(struct nilfs_sb_info *sbi,
                          struct nilfs_super_block *sbp)
 {
        sbi->s_mount_opt =
-               NILFS_MOUNT_ERRORS_CONT | NILFS_MOUNT_BARRIER;
+               NILFS_MOUNT_ERRORS_RO | NILFS_MOUNT_BARRIER;
 }
 
 static int nilfs_setup_super(struct nilfs_sb_info *sbi)
@@ -649,9 +618,7 @@ static int nilfs_setup_super(struct nilfs_sb_info *sbi)
        int mnt_count = le16_to_cpu(sbp->s_mnt_count);
 
        /* nilfs->sem must be locked by the caller. */
-       if (!(nilfs->ns_mount_state & NILFS_VALID_FS)) {
-               printk(KERN_WARNING "NILFS warning: mounting unchecked fs\n");
-       } else if (nilfs->ns_mount_state & NILFS_ERROR_FS) {
+       if (nilfs->ns_mount_state & NILFS_ERROR_FS) {
                printk(KERN_WARNING
                       "NILFS warning: mounting fs with errors\n");
 #if 0
@@ -758,12 +725,12 @@ nilfs_fill_super(struct super_block *sb, void *data, int silent,
        sb->s_export_op = &nilfs_export_ops;
        sb->s_root = NULL;
        sb->s_time_gran = 1;
+       sb->s_bdi = nilfs->ns_bdi;
+
+       err = load_nilfs(nilfs, sbi);
+       if (err)
+               goto failed_sbi;
 
-       if (!nilfs_loaded(nilfs)) {
-               err = load_nilfs(nilfs, sbi);
-               if (err)
-                       goto failed_sbi;
-       }
        cno = nilfs_last_cno(nilfs);
 
        if (sb->s_flags & MS_RDONLY) {
@@ -787,9 +754,7 @@ nilfs_fill_super(struct super_block *sb, void *data, int silent,
                                goto failed_sbi;
                        }
                        cno = sbi->s_snapshot_cno;
-               } else
-                       /* Read-only mount */
-                       sbi->s_snapshot_cno = cno;
+               }
        }
 
        err = nilfs_attach_checkpoint(sbi, cno);
@@ -831,12 +796,6 @@ nilfs_fill_super(struct super_block *sb, void *data, int silent,
                up_write(&nilfs->ns_sem);
        }
 
-       err = nilfs_mark_recovery_complete(sbi);
-       if (unlikely(err)) {
-               printk(KERN_ERR "NILFS: recovery failed.\n");
-               goto failed_root;
-       }
-
        down_write(&nilfs->ns_super_sem);
        if (!nilfs_test_opt(sbi, SNAPSHOT))
                nilfs->ns_current = sbi;
@@ -844,10 +803,6 @@ nilfs_fill_super(struct super_block *sb, void *data, int silent,
 
        return 0;
 
- failed_root:
-       dput(sb->s_root);
-       sb->s_root = NULL;
-
  failed_segctor:
        nilfs_detach_segment_constructor(sbi);
 
@@ -868,7 +823,7 @@ static int nilfs_remount(struct super_block *sb, int *flags, char *data)
        struct the_nilfs *nilfs = sbi->s_nilfs;
        unsigned long old_sb_flags;
        struct nilfs_mount_options old_opts;
-       int err;
+       int was_snapshot, err;
 
        lock_kernel();
 
@@ -876,6 +831,7 @@ static int nilfs_remount(struct super_block *sb, int *flags, char *data)
        old_sb_flags = sb->s_flags;
        old_opts.mount_opt = sbi->s_mount_opt;
        old_opts.snapshot_cno = sbi->s_snapshot_cno;
+       was_snapshot = nilfs_test_opt(sbi, SNAPSHOT);
 
        if (!parse_options(data, sb)) {
                err = -EINVAL;
@@ -883,12 +839,32 @@ static int nilfs_remount(struct super_block *sb, int *flags, char *data)
        }
        sb->s_flags = (sb->s_flags & ~MS_POSIXACL);
 
-       if ((*flags & MS_RDONLY) &&
-           sbi->s_snapshot_cno != old_opts.snapshot_cno) {
+       err = -EINVAL;
+       if (was_snapshot) {
+               if (!(*flags & MS_RDONLY)) {
+                       printk(KERN_ERR "NILFS (device %s): cannot remount "
+                              "snapshot read/write.\n",
+                              sb->s_id);
+                       goto restore_opts;
+               } else if (sbi->s_snapshot_cno != old_opts.snapshot_cno) {
+                       printk(KERN_ERR "NILFS (device %s): cannot "
+                              "remount to a different snapshot.\n",
+                              sb->s_id);
+                       goto restore_opts;
+               }
+       } else {
+               if (nilfs_test_opt(sbi, SNAPSHOT)) {
+                       printk(KERN_ERR "NILFS (device %s): cannot change "
+                              "a regular mount to a snapshot.\n",
+                              sb->s_id);
+                       goto restore_opts;
+               }
+       }
+
+       if (!nilfs_valid_fs(nilfs)) {
                printk(KERN_WARNING "NILFS (device %s): couldn't "
-                      "remount to a different snapshot. \n",
-                      sb->s_id);
-               err = -EINVAL;
+                      "remount because the filesystem is in an "
+                      "incomplete recovery state.\n", sb->s_id);
                goto restore_opts;
        }
 
@@ -899,9 +875,6 @@ static int nilfs_remount(struct super_block *sb, int *flags, char *data)
                nilfs_detach_segment_constructor(sbi);
                sb->s_flags |= MS_RDONLY;
 
-               sbi->s_snapshot_cno = nilfs_last_cno(nilfs);
-               /* nilfs_set_opt(sbi, SNAPSHOT); */
-
                /*
                 * Remounting a valid RW partition RDONLY, so set
                 * the RDONLY flag and then mark the partition as valid again.
@@ -920,24 +893,7 @@ static int nilfs_remount(struct super_block *sb, int *flags, char *data)
                 * store the current valid flag.  (It may have been changed
                 * by fsck since we originally mounted the partition.)
                 */
-               if (nilfs->ns_current && nilfs->ns_current != sbi) {
-                       printk(KERN_WARNING "NILFS (device %s): couldn't "
-                              "remount because an RW-mount exists.\n",
-                              sb->s_id);
-                       err = -EBUSY;
-                       goto restore_opts;
-               }
-               if (sbi->s_snapshot_cno != nilfs_last_cno(nilfs)) {
-                       printk(KERN_WARNING "NILFS (device %s): couldn't "
-                              "remount because the current RO-mount is not "
-                              "the latest one.\n",
-                              sb->s_id);
-                       err = -EINVAL;
-                       goto restore_opts;
-               }
                sb->s_flags &= ~MS_RDONLY;
-               nilfs_clear_opt(sbi, SNAPSHOT);
-               sbi->s_snapshot_cno = 0;
 
                err = nilfs_attach_segment_constructor(sbi);
                if (err)
@@ -946,8 +902,6 @@ static int nilfs_remount(struct super_block *sb, int *flags, char *data)
                down_write(&nilfs->ns_sem);
                nilfs_setup_super(sbi);
                up_write(&nilfs->ns_sem);
-
-               nilfs->ns_current = sbi;
        }
  out:
        up_write(&nilfs->ns_super_sem);
@@ -1033,10 +987,14 @@ nilfs_get_sb(struct file_system_type *fs_type, int flags,
 {
        struct nilfs_super_data sd;
        struct super_block *s;
+       fmode_t mode = FMODE_READ;
        struct the_nilfs *nilfs;
        int err, need_to_close = 1;
 
-       sd.bdev = open_bdev_exclusive(dev_name, flags, fs_type);
+       if (!(flags & MS_RDONLY))
+               mode |= FMODE_WRITE;
+
+       sd.bdev = open_bdev_exclusive(dev_name, mode, fs_type);
        if (IS_ERR(sd.bdev))
                return PTR_ERR(sd.bdev);
 
@@ -1103,10 +1061,12 @@ nilfs_get_sb(struct file_system_type *fs_type, int flags,
 
                /* New superblock instance created */
                s->s_flags = flags;
+               s->s_mode = mode;
                strlcpy(s->s_id, bdevname(sd.bdev, b), sizeof(s->s_id));
                sb_set_blocksize(s, block_size(sd.bdev));
 
-               err = nilfs_fill_super(s, data, flags & MS_VERBOSE, nilfs);
+               err = nilfs_fill_super(s, data, flags & MS_SILENT ? 1 : 0,
+                                      nilfs);
                if (err)
                        goto cancel_new;
 
@@ -1117,7 +1077,7 @@ nilfs_get_sb(struct file_system_type *fs_type, int flags,
        mutex_unlock(&nilfs->ns_mount_mutex);
        put_nilfs(nilfs);
        if (need_to_close)
-               close_bdev_exclusive(sd.bdev, flags);
+               close_bdev_exclusive(sd.bdev, mode);
        simple_set_mnt(mnt, s);
        return 0;
 
@@ -1125,7 +1085,7 @@ nilfs_get_sb(struct file_system_type *fs_type, int flags,
        mutex_unlock(&nilfs->ns_mount_mutex);
        put_nilfs(nilfs);
  failed:
-       close_bdev_exclusive(sd.bdev, flags);
+       close_bdev_exclusive(sd.bdev, mode);
 
        return err;
 
@@ -1133,10 +1093,9 @@ nilfs_get_sb(struct file_system_type *fs_type, int flags,
        /* Abandoning the newly allocated superblock */
        mutex_unlock(&nilfs->ns_mount_mutex);
        put_nilfs(nilfs);
-       up_write(&s->s_umount);
-       deactivate_super(s);
+       deactivate_locked_super(s);
        /*
-        * deactivate_super() invokes close_bdev_exclusive().
+        * deactivate_locked_super() invokes close_bdev_exclusive().
         * We must finish all post-cleaning before this call;
         * put_nilfs() needs the block device.
         */
@@ -1151,54 +1110,93 @@ struct file_system_type nilfs_fs_type = {
        .fs_flags = FS_REQUIRES_DEV,
 };
 
-static int __init init_nilfs_fs(void)
+static void nilfs_inode_init_once(void *obj)
 {
-       int err;
-
-       err = nilfs_init_inode_cache();
-       if (err)
-               goto failed;
+       struct nilfs_inode_info *ii = obj;
 
-       err = nilfs_init_transaction_cache();
-       if (err)
-               goto failed_inode_cache;
+       INIT_LIST_HEAD(&ii->i_dirty);
+#ifdef CONFIG_NILFS_XATTR
+       init_rwsem(&ii->xattr_sem);
+#endif
+       nilfs_btnode_cache_init_once(&ii->i_btnode_cache);
+       ii->i_bmap = (struct nilfs_bmap *)&ii->i_bmap_union;
+       inode_init_once(&ii->vfs_inode);
+}
 
-       err = nilfs_init_segbuf_cache();
-       if (err)
-               goto failed_transaction_cache;
+static void nilfs_segbuf_init_once(void *obj)
+{
+       memset(obj, 0, sizeof(struct nilfs_segment_buffer));
+}
 
-       err = nilfs_btree_path_cache_init();
-       if (err)
-               goto failed_segbuf_cache;
+static void nilfs_destroy_cachep(void)
+{
+        if (nilfs_inode_cachep)
+               kmem_cache_destroy(nilfs_inode_cachep);
+        if (nilfs_transaction_cachep)
+               kmem_cache_destroy(nilfs_transaction_cachep);
+        if (nilfs_segbuf_cachep)
+               kmem_cache_destroy(nilfs_segbuf_cachep);
+        if (nilfs_btree_path_cache)
+               kmem_cache_destroy(nilfs_btree_path_cache);
+}
 
-       err = register_filesystem(&nilfs_fs_type);
-       if (err)
-               goto failed_btree_path_cache;
+static int __init nilfs_init_cachep(void)
+{
+       nilfs_inode_cachep = kmem_cache_create("nilfs2_inode_cache",
+                       sizeof(struct nilfs_inode_info), 0,
+                       SLAB_RECLAIM_ACCOUNT, nilfs_inode_init_once);
+       if (!nilfs_inode_cachep)
+               goto fail;
+
+       nilfs_transaction_cachep = kmem_cache_create("nilfs2_transaction_cache",
+                       sizeof(struct nilfs_transaction_info), 0,
+                       SLAB_RECLAIM_ACCOUNT, NULL);
+       if (!nilfs_transaction_cachep)
+               goto fail;
+
+       nilfs_segbuf_cachep = kmem_cache_create("nilfs2_segbuf_cache",
+                       sizeof(struct nilfs_segment_buffer), 0,
+                       SLAB_RECLAIM_ACCOUNT, nilfs_segbuf_init_once);
+       if (!nilfs_segbuf_cachep)
+               goto fail;
+
+       nilfs_btree_path_cache = kmem_cache_create("nilfs2_btree_path_cache",
+                       sizeof(struct nilfs_btree_path) * NILFS_BTREE_LEVEL_MAX,
+                       0, 0, NULL);
+       if (!nilfs_btree_path_cache)
+               goto fail;
 
        return 0;
 
- failed_btree_path_cache:
-       nilfs_btree_path_cache_destroy();
+fail:
+       nilfs_destroy_cachep();
+       return -ENOMEM;
+}
 
- failed_segbuf_cache:
-       nilfs_destroy_segbuf_cache();
+static int __init init_nilfs_fs(void)
+{
+       int err;
 
- failed_transaction_cache:
-       nilfs_destroy_transaction_cache();
+       err = nilfs_init_cachep();
+       if (err)
+               goto fail;
+
+       err = register_filesystem(&nilfs_fs_type);
+       if (err)
+               goto free_cachep;
 
- failed_inode_cache:
-       nilfs_destroy_inode_cache();
+       printk(KERN_INFO "NILFS version 2 loaded\n");
+       return 0;
 
- failed:
+free_cachep:
+       nilfs_destroy_cachep();
+fail:
        return err;
 }
 
 static void __exit exit_nilfs_fs(void)
 {
-       nilfs_destroy_segbuf_cache();
-       nilfs_destroy_transaction_cache();
-       nilfs_destroy_inode_cache();
-       nilfs_btree_path_cache_destroy();
+       nilfs_destroy_cachep();
        unregister_filesystem(&nilfs_fs_type);
 }