📄 super.c
字号:
/* -*- mode: c; c-basic-offset: 8; -*- * vim: noexpandtab sw=8 ts=8 sts=0: * * super.c * * load/unload driver, mount/dismount volumes * * Copyright (C) 2002, 2004 Oracle. All rights reserved. * * 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., 59 Temple Place - Suite 330, * Boston, MA 021110-1307, USA. */#include <linux/module.h>#include <linux/fs.h>#include <linux/types.h>#include <linux/slab.h>#include <linux/highmem.h>#include <linux/utsname.h>#include <linux/init.h>#include <linux/random.h>#include <linux/statfs.h>#include <linux/moduleparam.h>#include <linux/blkdev.h>#include <linux/socket.h>#include <linux/inet.h>#include <linux/parser.h>#include <linux/crc32.h>#include <linux/debugfs.h>#include <linux/mount.h>#include <cluster/nodemanager.h>#define MLOG_MASK_PREFIX ML_SUPER#include <cluster/masklog.h>#include "ocfs2.h"/* this should be the only file to include a version 1 header */#include "ocfs1_fs_compat.h"#include "alloc.h"#include "dlmglue.h"#include "export.h"#include "extent_map.h"#include "heartbeat.h"#include "inode.h"#include "journal.h"#include "localalloc.h"#include "namei.h"#include "slot_map.h"#include "super.h"#include "sysfile.h"#include "uptodate.h"#include "ver.h"#include "buffer_head_io.h"static struct kmem_cache *ocfs2_inode_cachep = NULL;/* OCFS2 needs to schedule several differnt types of work which * require cluster locking, disk I/O, recovery waits, etc. Since these * types of work tend to be heavy we avoid using the kernel events * workqueue and schedule on our own. */struct workqueue_struct *ocfs2_wq = NULL;static struct dentry *ocfs2_debugfs_root = NULL;MODULE_AUTHOR("Oracle");MODULE_LICENSE("GPL");struct mount_options{ unsigned long commit_interval; unsigned long mount_opt; unsigned int atime_quantum; signed short slot; unsigned int localalloc_opt;};static int ocfs2_parse_options(struct super_block *sb, char *options, struct mount_options *mopt, int is_remount);static int ocfs2_show_options(struct seq_file *s, struct vfsmount *mnt);static void ocfs2_put_super(struct super_block *sb);static int ocfs2_mount_volume(struct super_block *sb);static int ocfs2_remount(struct super_block *sb, int *flags, char *data);static void ocfs2_dismount_volume(struct super_block *sb, int mnt_err);static int ocfs2_initialize_mem_caches(void);static void ocfs2_free_mem_caches(void);static void ocfs2_delete_osb(struct ocfs2_super *osb);#ifdef STATFS_TAKES_SBstatic int ocfs2_statfs(struct super_block *sb, struct kstatfs *buf)#elsestatic int ocfs2_statfs(struct dentry *dentry, struct kstatfs *buf);#endifstatic int ocfs2_sync_fs(struct super_block *sb, int wait);static int ocfs2_init_global_system_inodes(struct ocfs2_super *osb);static int ocfs2_init_local_system_inodes(struct ocfs2_super *osb);static void ocfs2_release_system_inodes(struct ocfs2_super *osb);static int ocfs2_fill_local_node_info(struct ocfs2_super *osb);static int ocfs2_check_volume(struct ocfs2_super *osb);static int ocfs2_verify_volume(struct ocfs2_dinode *di, struct buffer_head *bh, u32 sectsize);static int ocfs2_initialize_super(struct super_block *sb, struct buffer_head *bh, int sector_size);static int ocfs2_get_sector(struct super_block *sb, struct buffer_head **bh, int block, int sect_size);static void ocfs2_write_super(struct super_block *sb);static struct inode *ocfs2_alloc_inode(struct super_block *sb);static void ocfs2_destroy_inode(struct inode *inode);#ifdef SOP_IS_NOT_CONSTstatic struct super_operations ocfs2_sops = {#elsestatic const struct super_operations ocfs2_sops = {#endif .statfs = ocfs2_statfs, .alloc_inode = ocfs2_alloc_inode, .destroy_inode = ocfs2_destroy_inode, .drop_inode = ocfs2_drop_inode, .clear_inode = ocfs2_clear_inode, .delete_inode = ocfs2_delete_inode, .sync_fs = ocfs2_sync_fs, .write_super = ocfs2_write_super, .put_super = ocfs2_put_super, .remount_fs = ocfs2_remount, .show_options = ocfs2_show_options,};#ifdef OCFS2_ORACORE_WORKAROUNDS# define OCFS_SUPER_MAGIC 0xa156f7eb#endifenum { Opt_barrier, Opt_err_panic, Opt_err_ro, Opt_intr, Opt_nointr, Opt_hb_none, Opt_hb_local, Opt_data_ordered, Opt_data_writeback, Opt_atime_quantum, Opt_slot, Opt_commit, Opt_localalloc, Opt_localflocks,#ifdef OCFS2_ORACORE_WORKAROUNDS Opt_datavolume,#endif Opt_err,};static match_table_t tokens = { {Opt_barrier, "barrier=%u"}, {Opt_err_panic, "errors=panic"}, {Opt_err_ro, "errors=remount-ro"}, {Opt_intr, "intr"}, {Opt_nointr, "nointr"}, {Opt_hb_none, OCFS2_HB_NONE}, {Opt_hb_local, OCFS2_HB_LOCAL}, {Opt_data_ordered, "data=ordered"}, {Opt_data_writeback, "data=writeback"}, {Opt_atime_quantum, "atime_quantum=%u"}, {Opt_slot, "preferred_slot=%u"}, {Opt_commit, "commit=%u"}, {Opt_localalloc, "localalloc=%d"}, {Opt_localflocks, "localflocks"},#ifdef OCFS2_ORACORE_WORKAROUNDS {Opt_datavolume, "datavolume"},#endif {Opt_err, NULL}};/* * write_super and sync_fs ripped right out of ext3. */static void ocfs2_write_super(struct super_block *sb){ if (mutex_trylock(&sb->s_lock) != 0) BUG(); sb->s_dirt = 0;}static int ocfs2_sync_fs(struct super_block *sb, int wait){ int status; tid_t target; struct ocfs2_super *osb = OCFS2_SB(sb); sb->s_dirt = 0; if (ocfs2_is_hard_readonly(osb)) return -EROFS; if (wait) { status = ocfs2_flush_truncate_log(osb); if (status < 0) mlog_errno(status); } else { ocfs2_schedule_truncate_log_flush(osb, 0); } if (journal_start_commit(OCFS2_SB(sb)->journal->j_journal, &target)) { if (wait) log_wait_commit(OCFS2_SB(sb)->journal->j_journal, target); } return 0;}static int ocfs2_init_global_system_inodes(struct ocfs2_super *osb){ struct inode *new = NULL; int status = 0; int i; mlog_entry_void(); new = ocfs2_iget(osb, osb->root_blkno, OCFS2_FI_FLAG_SYSFILE, 0); if (IS_ERR(new)) { status = PTR_ERR(new); mlog_errno(status); goto bail; } osb->root_inode = new; new = ocfs2_iget(osb, osb->system_dir_blkno, OCFS2_FI_FLAG_SYSFILE, 0); if (IS_ERR(new)) { status = PTR_ERR(new); mlog_errno(status); goto bail; } osb->sys_root_inode = new; for (i = OCFS2_FIRST_ONLINE_SYSTEM_INODE; i <= OCFS2_LAST_GLOBAL_SYSTEM_INODE; i++) { new = ocfs2_get_system_file_inode(osb, i, osb->slot_num); if (!new) { ocfs2_release_system_inodes(osb); status = -EINVAL; mlog_errno(status); /* FIXME: Should ERROR_RO_FS */ mlog(ML_ERROR, "Unable to load system inode %d, " "possibly corrupt fs?", i); goto bail; } // the array now has one ref, so drop this one iput(new); }bail: mlog_exit(status); return status;}static int ocfs2_init_local_system_inodes(struct ocfs2_super *osb){ struct inode *new = NULL; int status = 0; int i; mlog_entry_void(); for (i = OCFS2_LAST_GLOBAL_SYSTEM_INODE + 1; i < NUM_SYSTEM_INODES; i++) { new = ocfs2_get_system_file_inode(osb, i, osb->slot_num); if (!new) { ocfs2_release_system_inodes(osb); status = -EINVAL; mlog(ML_ERROR, "status=%d, sysfile=%d, slot=%d\n", status, i, osb->slot_num); goto bail; } /* the array now has one ref, so drop this one */ iput(new); }bail: mlog_exit(status); return status;}static void ocfs2_release_system_inodes(struct ocfs2_super *osb){ int i; struct inode *inode; mlog_entry_void(); for (i = 0; i < NUM_SYSTEM_INODES; i++) { inode = osb->system_inodes[i]; if (inode) { iput(inode); osb->system_inodes[i] = NULL; } } inode = osb->sys_root_inode; if (inode) { iput(inode); osb->sys_root_inode = NULL; } inode = osb->root_inode; if (inode) { iput(inode); osb->root_inode = NULL; } mlog_exit(0);}/* We're allocating fs objects, use GFP_NOFS */static struct inode *ocfs2_alloc_inode(struct super_block *sb){ struct ocfs2_inode_info *oi; oi = kmem_cache_alloc(ocfs2_inode_cachep, GFP_NOFS); if (!oi) return NULL; return &oi->vfs_inode;}static void ocfs2_destroy_inode(struct inode *inode){ kmem_cache_free(ocfs2_inode_cachep, OCFS2_I(inode));}static unsigned long long ocfs2_max_file_offset(unsigned int bbits, unsigned int cbits){ unsigned int bytes = 1 << cbits; unsigned int trim = bytes; unsigned int bitshift = 32; /* * i_size and all block offsets in ocfs2 are always 64 bits * wide. i_clusters is 32 bits, in cluster-sized units. So on * 64 bit platforms, cluster size will be the limiting factor. */#if BITS_PER_LONG == 32# if defined(CONFIG_LBD) BUILD_BUG_ON(sizeof(sector_t) != 8); /* * We might be limited by page cache size. */ if (bytes > PAGE_CACHE_SIZE) { bytes = PAGE_CACHE_SIZE; trim = 1; /* * Shift by 31 here so that we don't get larger than * MAX_LFS_FILESIZE */ bitshift = 31; }# else /* * We are limited by the size of sector_t. Use block size, as * that's what we expose to the VFS. */ bytes = 1 << bbits; trim = 1; bitshift = 31;# endif#endif /* * Trim by a whole cluster when we can actually approach the * on-disk limits. Otherwise we can overflow i_clusters when * an extent start is at the max offset. */ return (((unsigned long long)bytes) << bitshift) - trim;}static int ocfs2_remount(struct super_block *sb, int *flags, char *data){ int incompat_features; int ret = 0; struct mount_options parsed_options; struct ocfs2_super *osb = OCFS2_SB(sb); if (!ocfs2_parse_options(sb, data, &parsed_options, 1)) { ret = -EINVAL; goto out; } if ((osb->s_mount_opt & OCFS2_MOUNT_HB_LOCAL) != (parsed_options.mount_opt & OCFS2_MOUNT_HB_LOCAL)) { ret = -EINVAL; mlog(ML_ERROR, "Cannot change heartbeat mode on remount\n"); goto out; } if ((osb->s_mount_opt & OCFS2_MOUNT_DATA_WRITEBACK) != (parsed_options.mount_opt & OCFS2_MOUNT_DATA_WRITEBACK)) { ret = -EINVAL; mlog(ML_ERROR, "Cannot change data mode on remount\n"); goto out; } /* We're going to/from readonly mode. */ if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY)) { /* Lock here so the check of HARD_RO and the potential * setting of SOFT_RO is atomic. */ spin_lock(&osb->osb_lock); if (osb->osb_flags & OCFS2_OSB_HARD_RO) { mlog(ML_ERROR, "Remount on readonly device is forbidden.\n"); ret = -EROFS; goto unlock_osb; } if (*flags & MS_RDONLY) { mlog(0, "Going to ro mode.\n"); sb->s_flags |= MS_RDONLY; osb->osb_flags |= OCFS2_OSB_SOFT_RO; } else { mlog(0, "Making ro filesystem writeable.\n"); if (osb->osb_flags & OCFS2_OSB_ERROR_FS) { mlog(ML_ERROR, "Cannot remount RDWR " "filesystem due to previous errors.\n"); ret = -EROFS; goto unlock_osb; } incompat_features = OCFS2_HAS_RO_COMPAT_FEATURE(sb, ~OCFS2_FEATURE_RO_COMPAT_SUPP); if (incompat_features) { mlog(ML_ERROR, "Cannot remount RDWR because " "of unsupported optional features " "(%x).\n", incompat_features); ret = -EINVAL; goto unlock_osb; } sb->s_flags &= ~MS_RDONLY; osb->osb_flags &= ~OCFS2_OSB_SOFT_RO; }unlock_osb: spin_unlock(&osb->osb_lock); } if (!ret) { /* Only save off the new mount options in case of a successful * remount. */ osb->s_mount_opt = parsed_options.mount_opt; osb->s_atime_quantum = parsed_options.atime_quantum; osb->preferred_slot = parsed_options.slot; if (parsed_options.commit_interval) osb->osb_commit_interval = parsed_options.commit_interval; if (!ocfs2_is_hard_readonly(osb)) ocfs2_set_journal_params(osb); }out: return ret;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -