Commit 783f6184 authored by Ryusuke Konishi's avatar Ryusuke Konishi Committed by Linus Torvalds
Browse files

nilfs2: super block operations



This adds super block operations for the nilfs2 file system.

Signed-off-by: default avatarRyusuke Konishi <konishi.ryusuke@lab.ntt.co.jp>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 8a9d2191
/*
* super.c - NILFS module and super block management.
*
* Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* Written by Ryusuke Konishi <ryusuke@osrg.net>
*/
/*
* linux/fs/ext2/super.c
*
* Copyright (C) 1992, 1993, 1994, 1995
* Remy Card (card@masi.ibp.fr)
* Laboratoire MASI - Institut Blaise Pascal
* Universite Pierre et Marie Curie (Paris VI)
*
* from
*
* linux/fs/minix/inode.c
*
* Copyright (C) 1991, 1992 Linus Torvalds
*
* Big-endian to little-endian byte-swapping/bitmaps by
* David S. Miller (davem@caip.rutgers.edu), 1995
*/
#include <linux/module.h>
#include <linux/string.h>
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/blkdev.h>
#include <linux/parser.h>
#include <linux/random.h>
#include <linux/crc32.h>
#include <linux/smp_lock.h>
#include <linux/vfs.h>
#include <linux/writeback.h>
#include <linux/kobject.h>
#include <linux/exportfs.h>
#include "nilfs.h"
#include "mdt.h"
#include "alloc.h"
#include "page.h"
#include "cpfile.h"
#include "ifile.h"
#include "dat.h"
#include "segment.h"
#include "segbuf.h"
MODULE_AUTHOR("NTT Corp.");
MODULE_DESCRIPTION("A New Implementation of the Log-structured Filesystem "
"(NILFS)");
MODULE_VERSION(NILFS_VERSION);
MODULE_LICENSE("GPL");
static int nilfs_remount(struct super_block *sb, int *flags, char *data);
static int test_exclusive_mount(struct file_system_type *fs_type,
struct block_device *bdev, int flags);
/**
* nilfs_error() - report failure condition on a filesystem
*
* nilfs_error() sets an ERROR_FS flag on the superblock as well as
* reporting an error message. It should be called when NILFS detects
* incoherences or defects of meta data on disk. As for sustainable
* errors such as a single-shot I/O error, nilfs_warning() or the printk()
* function should be used instead.
*
* The segment constructor must not call this function because it can
* kill itself.
*/
void nilfs_error(struct super_block *sb, const char *function,
const char *fmt, ...)
{
struct nilfs_sb_info *sbi = NILFS_SB(sb);
va_list args;
va_start(args, fmt);
printk(KERN_CRIT "NILFS error (device %s): %s: ", sb->s_id, function);
vprintk(fmt, args);
printk("\n");
va_end(args);
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;
nilfs->ns_sbp->s_state |= cpu_to_le16(NILFS_ERROR_FS);
nilfs_commit_super(sbi);
}
up_write(&nilfs->ns_sem);
if (nilfs_test_opt(sbi, ERRORS_RO)) {
printk(KERN_CRIT "Remounting filesystem read-only\n");
sb->s_flags |= MS_RDONLY;
}
}
if (nilfs_test_opt(sbi, ERRORS_PANIC))
panic("NILFS (device %s): panic forced after error\n",
sb->s_id);
}
void nilfs_warning(struct super_block *sb, const char *function,
const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
printk(KERN_WARNING "NILFS warning (device %s): %s: ",
sb->s_id, function);
vprintk(fmt, args);
printk("\n");
va_end(args);
}
static struct kmem_cache *nilfs_inode_cachep;
struct inode *nilfs_alloc_inode(struct super_block *sb)
{
struct nilfs_inode_info *ii;
ii = kmem_cache_alloc(nilfs_inode_cachep, GFP_NOFS);
if (!ii)
return NULL;
ii->i_bh = NULL;
ii->i_state = 0;
ii->vfs_inode.i_version = 1;
nilfs_btnode_cache_init(&ii->i_btnode_cache);
return &ii->vfs_inode;
}
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);
struct nilfs_transaction_info ti;
struct nilfs_sb_info *sbi = NILFS_SB(inode->i_sb);
#ifdef CONFIG_NILFS_POSIX_ACL
if (ii->i_acl && ii->i_acl != NILFS_ACL_NOT_CACHED) {
posix_acl_release(ii->i_acl);
ii->i_acl = NILFS_ACL_NOT_CACHED;
}
if (ii->i_default_acl && ii->i_default_acl != NILFS_ACL_NOT_CACHED) {
posix_acl_release(ii->i_default_acl);
ii->i_default_acl = NILFS_ACL_NOT_CACHED;
}
#endif
/*
* Free resources allocated in nilfs_read_inode(), here.
*/
nilfs_transaction_begin(inode->i_sb, &ti, 0);
spin_lock(&sbi->s_inode_lock);
if (!list_empty(&ii->i_dirty))
list_del_init(&ii->i_dirty);
brelse(ii->i_bh);
ii->i_bh = NULL;
spin_unlock(&sbi->s_inode_lock);
if (test_bit(NILFS_I_BMAP, &ii->i_state))
nilfs_bmap_clear(ii->i_bmap);
nilfs_btnode_cache_clear(&ii->i_btnode_cache);
nilfs_transaction_end(inode->i_sb, 0);
}
/**
* nilfs_update_last_segment - change pointer to the latest segment
* @sbi: nilfs_sb_info
* @update_cno: flag whether to update checkpoint number.
*
* nilfs_update_last_segment() changes information in the super block
* after a partial segment is written out successfully. The super
* block is marked dirty. It will be written out at the next VFS sync
* operations such as sync_supers() and generic_shutdown_super().
*/
void nilfs_update_last_segment(struct nilfs_sb_info *sbi, int update_cno)
{
struct the_nilfs *nilfs = sbi->s_nilfs;
struct nilfs_super_block *sbp = nilfs->ns_sbp;
/* nilfs->sem must be locked by the caller. */
spin_lock(&nilfs->ns_last_segment_lock);
if (update_cno)
nilfs->ns_last_cno = nilfs->ns_cno++;
sbp->s_last_seq = cpu_to_le64(nilfs->ns_last_seq);
sbp->s_last_pseg = cpu_to_le64(nilfs->ns_last_pseg);
sbp->s_last_cno = cpu_to_le64(nilfs->ns_last_cno);
spin_unlock(&nilfs->ns_last_segment_lock);
sbi->s_super->s_dirt = 1; /* must be set if delaying the call of
nilfs_commit_super() */
}
static int nilfs_sync_super(struct nilfs_sb_info *sbi)
{
struct the_nilfs *nilfs = sbi->s_nilfs;
int err;
int barrier_done = 0;
if (nilfs_test_opt(sbi, BARRIER)) {
set_buffer_ordered(nilfs->ns_sbh);
barrier_done = 1;
}
retry:
set_buffer_dirty(nilfs->ns_sbh);
err = sync_dirty_buffer(nilfs->ns_sbh);
if (err == -EOPNOTSUPP && barrier_done) {
nilfs_warning(sbi->s_super, __func__,
"barrier-based sync failed. "
"disabling barriers\n");
nilfs_clear_opt(sbi, BARRIER);
barrier_done = 0;
clear_buffer_ordered(nilfs->ns_sbh);
goto retry;
}
if (unlikely(err))
printk(KERN_ERR
"NILFS: unable to write superblock (err=%d)\n", err);
else {
nilfs_dispose_used_segments(nilfs);
clear_nilfs_discontinued(nilfs);
}
return err;
}
int nilfs_commit_super(struct nilfs_sb_info *sbi)
{
struct the_nilfs *nilfs = sbi->s_nilfs;
struct nilfs_super_block *sbp = nilfs->ns_sbp;
sector_t nfreeblocks;
int err;
/* nilfs->sem must be locked by the caller. */
err = nilfs_count_free_blocks(nilfs, &nfreeblocks);
if (unlikely(err)) {
printk(KERN_ERR "NILFS: failed to count free blocks\n");
return err;
}
sbp->s_free_blocks_count = cpu_to_le64(nfreeblocks);
sbp->s_wtime = cpu_to_le64(get_seconds());
sbp->s_sum = 0;
sbp->s_sum = crc32_le(nilfs->ns_crc_seed, (unsigned char *)sbp,
le16_to_cpu(sbp->s_bytes));
sbi->s_super->s_dirt = 0;
return nilfs_sync_super(sbi);
}
static void nilfs_put_super(struct super_block *sb)
{
struct nilfs_sb_info *sbi = NILFS_SB(sb);
struct the_nilfs *nilfs = sbi->s_nilfs;
nilfs_detach_segment_constructor(sbi);
if (!(sb->s_flags & MS_RDONLY)) {
down_write(&nilfs->ns_sem);
nilfs->ns_sbp->s_state = cpu_to_le16(nilfs->ns_mount_state);
nilfs_commit_super(sbi);
up_write(&nilfs->ns_sem);
}
nilfs_detach_checkpoint(sbi);
put_nilfs(sbi->s_nilfs);
sbi->s_super = NULL;
sb->s_fs_info = NULL;
kfree(sbi);
}
/**
* nilfs_write_super - write super block(s) of NILFS
* @sb: super_block
*
* nilfs_write_super() gets a fs-dependent lock, writes super block(s), and
* clears s_dirt. This function is called in the section protected by
* lock_super().
*
* The s_dirt flag is managed by each filesystem and we protect it by ns_sem
* of the struct the_nilfs. Lock order must be as follows:
*
* 1. lock_super()
* 2. down_write(&nilfs->ns_sem)
*
* Inside NILFS, locking ns_sem is enough to protect s_dirt and the buffer
* of the super block (nilfs->ns_sbp).
*
* In most cases, VFS functions call lock_super() before calling these
* methods. So we must be careful not to bring on deadlocks when using
* lock_super(); see generic_shutdown_super(), write_super(), and so on.
*
* Note that order of lock_kernel() and lock_super() depends on contexts
* of VFS. We should also note that lock_kernel() can be used in its
* protective section and only the outermost one has an effect.
*/
static void nilfs_write_super(struct super_block *sb)
{
struct nilfs_sb_info *sbi = NILFS_SB(sb);
struct the_nilfs *nilfs = sbi->s_nilfs;
down_write(&nilfs->ns_sem);
if (!(sb->s_flags & MS_RDONLY))
nilfs_commit_super(sbi);
sb->s_dirt = 0;
up_write(&nilfs->ns_sem);
}
static int nilfs_sync_fs(struct super_block *sb, int wait)
{
int err = 0;
/* This function is called when super block should be written back */
if (wait)
err = nilfs_construct_segment(sb);
return err;
}
int nilfs_attach_checkpoint(struct nilfs_sb_info *sbi, __u64 cno)
{
struct the_nilfs *nilfs = sbi->s_nilfs;
struct nilfs_checkpoint *raw_cp;
struct buffer_head *bh_cp;
int err;
down_write(&nilfs->ns_sem);
list_add(&sbi->s_list, &nilfs->ns_supers);
up_write(&nilfs->ns_sem);
sbi->s_ifile = nilfs_mdt_new(
nilfs, sbi->s_super, NILFS_IFILE_INO, NILFS_IFILE_GFP);
if (!sbi->s_ifile)
return -ENOMEM;
err = nilfs_palloc_init_blockgroup(sbi->s_ifile, nilfs->ns_inode_size);
if (unlikely(err))
goto failed;
err = nilfs_cpfile_get_checkpoint(nilfs->ns_cpfile, cno, 0, &raw_cp,
&bh_cp);
if (unlikely(err)) {
if (err == -ENOENT || err == -EINVAL) {
printk(KERN_ERR
"NILFS: Invalid checkpoint "
"(checkpoint number=%llu)\n",
(unsigned long long)cno);
err = -EINVAL;
}
goto failed;
}
err = nilfs_read_inode_common(sbi->s_ifile, &raw_cp->cp_ifile_inode);
if (unlikely(err))
goto failed_bh;
atomic_set(&sbi->s_inodes_count, le64_to_cpu(raw_cp->cp_inodes_count));
atomic_set(&sbi->s_blocks_count, le64_to_cpu(raw_cp->cp_blocks_count));
nilfs_cpfile_put_checkpoint(nilfs->ns_cpfile, cno, bh_cp);
return 0;
failed_bh:
nilfs_cpfile_put_checkpoint(nilfs->ns_cpfile, cno, bh_cp);
failed:
nilfs_mdt_destroy(sbi->s_ifile);
sbi->s_ifile = NULL;
down_write(&nilfs->ns_sem);
list_del_init(&sbi->s_list);
up_write(&nilfs->ns_sem);
return err;
}
void nilfs_detach_checkpoint(struct nilfs_sb_info *sbi)
{
struct the_nilfs *nilfs = sbi->s_nilfs;
nilfs_mdt_clear(sbi->s_ifile);
nilfs_mdt_destroy(sbi->s_ifile);
sbi->s_ifile = NULL;
down_write(&nilfs->ns_sem);
list_del_init(&sbi->s_list);
up_write(&nilfs->ns_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);
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;
struct nilfs_sb_info *sbi = NILFS_SB(sb);
unsigned long long blocks;
unsigned long overhead;
unsigned long nrsvblocks;
sector_t nfreeblocks;
struct the_nilfs *nilfs = sbi->s_nilfs;
int err;
/*
* Compute all of the segment blocks
*
* The blocks before first segment and after last segment
* are excluded.
*/
blocks = nilfs->ns_blocks_per_segment * nilfs->ns_nsegments
- nilfs->ns_first_data_block;
nrsvblocks = nilfs->ns_nrsvsegs * nilfs->ns_blocks_per_segment;
/*
* Compute the overhead
*
* When distributing meta data blocks outside semgent structure,
* We must count them as the overhead.
*/
overhead = 0;
err = nilfs_count_free_blocks(nilfs, &nfreeblocks);
if (unlikely(err))
return err;
buf->f_type = NILFS_SUPER_MAGIC;
buf->f_bsize = sb->s_blocksize;
buf->f_blocks = blocks - overhead;
buf->f_bfree = nfreeblocks;
buf->f_bavail = (buf->f_bfree >= nrsvblocks) ?
(buf->f_bfree - nrsvblocks) : 0;
buf->f_files = atomic_read(&sbi->s_inodes_count);
buf->f_ffree = 0; /* nilfs_count_free_inodes(sb); */
buf->f_namelen = NILFS_NAME_LEN;
return 0;
}
static struct super_operations nilfs_sops = {
.alloc_inode = nilfs_alloc_inode,
.destroy_inode = nilfs_destroy_inode,
.dirty_inode = nilfs_dirty_inode,
/* .write_inode = nilfs_write_inode, */
/* .put_inode = nilfs_put_inode, */
/* .drop_inode = nilfs_drop_inode, */
.delete_inode = nilfs_delete_inode,
.put_super = nilfs_put_super,
.write_super = nilfs_write_super,
.sync_fs = nilfs_sync_fs,
/* .write_super_lockfs */
/* .unlockfs */
.statfs = nilfs_statfs,
.remount_fs = nilfs_remount,
.clear_inode = nilfs_clear_inode,
/* .umount_begin */
/* .show_options */
};
static struct inode *
nilfs_nfs_get_inode(struct super_block *sb, u64 ino, u32 generation)
{
struct inode *inode;
if (ino < NILFS_FIRST_INO(sb) && ino != NILFS_ROOT_INO &&
ino != NILFS_SKETCH_INO)
return ERR_PTR(-ESTALE);
inode = nilfs_iget(sb, ino);
if (IS_ERR(inode))
return ERR_CAST(inode);
if (generation && inode->i_generation != generation) {
iput(inode);
return ERR_PTR(-ESTALE);
}
return inode;
}
static struct dentry *
nilfs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
int fh_type)
{
return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
nilfs_nfs_get_inode);
}
static struct dentry *
nilfs_fh_to_parent(struct super_block *sb, struct fid *fid, int fh_len,
int fh_type)
{
return generic_fh_to_parent(sb, fid, fh_len, fh_type,
nilfs_nfs_get_inode);
}
static struct export_operations nilfs_export_ops = {
.fh_to_dentry = nilfs_fh_to_dentry,
.fh_to_parent = nilfs_fh_to_parent,
.get_parent = nilfs_get_parent,
};
enum {
Opt_err_cont, Opt_err_panic, Opt_err_ro,
Opt_barrier, Opt_snapshot, Opt_order,
Opt_err,
};
static match_table_t tokens = {
{Opt_err_cont, "errors=continue"},
{Opt_err_panic, "errors=panic"},
{Opt_err_ro, "errors=remount-ro"},
{Opt_barrier, "barrier=%s"},
{Opt_snapshot, "cp=%u"},
{Opt_order, "order=%s"},
{Opt_err, NULL}
};
static int match_bool(substring_t *s, int *result)
{
int len = s->to - s->from;
if (strncmp(s->from, "on", len) == 0)
*result = 1;
else if (strncmp(s->from, "off", len) == 0)
*result = 0;
else
return 1;
return 0;
}
static int parse_options(char *options, struct super_block *sb)
{
struct nilfs_sb_info *sbi = NILFS_SB(sb);
char *p;
substring_t args[MAX_OPT_ARGS];
int option;
if (!options)
return 1;
while ((p = strsep(&options, ",")) != NULL) {
int token;
if (!*p)
continue;
token = match_token(p, tokens, args);
switch (token) {
case Opt_barrier:
if (match_bool(&args[0], &option))
return 0;
if (option)
nilfs_set_opt(sbi, BARRIER);
else
nilfs_clear_opt(sbi, BARRIER);
break;
case Opt_order:
if (strcmp(args[0].from, "relaxed") == 0)
/* Ordered data semantics */
nilfs_clear_opt(sbi, STRICT_ORDER);
else if (strcmp(args[0].from, "strict") == 0)
/* Strict in-order semantics */
nilfs_set_opt(sbi, STRICT_ORDER);
else
return 0;
break;
case Opt_err_panic:
nilfs_write_opt(sbi, ERROR_MODE, ERRORS_PANIC);
break;
case Opt_err_ro:
nilfs_write_opt(sbi, ERROR_MODE, ERRORS_RO);
break;
case Opt_err_cont:
nilfs_write_opt(sbi, ERROR_MODE, ERRORS_CONT);
break;