⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 super.c

📁 linux 内核源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
/* -*- 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 "vote.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	mount_opt;	unsigned int	atime_quantum;	signed short	slot;};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);static int ocfs2_statfs(struct dentry *dentry, struct kstatfs *buf);static 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);static const struct super_operations ocfs2_sops = {	.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,};enum {	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_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_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);	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);	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 (!ocfs2_is_hard_readonly(osb))			ocfs2_set_journal_params(osb);	}out:	return ret;}static int ocfs2_sb_probe(struct super_block *sb,			  struct buffer_head **bh,			  int *sector_size){	int status, tmpstat;	struct ocfs1_vol_disk_hdr *hdr;	struct ocfs2_dinode *di;	int blksize;	*bh = NULL;	/* may be > 512 */	*sector_size = bdev_hardsect_size(sb->s_bdev);	if (*sector_size > OCFS2_MAX_BLOCKSIZE) {		mlog(ML_ERROR, "Hardware sector size too large: %d (max=%d)\n",		     *sector_size, OCFS2_MAX_BLOCKSIZE);		status = -EINVAL;		goto bail;	}	/* Can this really happen? */	if (*sector_size < OCFS2_MIN_BLOCKSIZE)		*sector_size = OCFS2_MIN_BLOCKSIZE;	/* check block zero for old format */	status = ocfs2_get_sector(sb, bh, 0, *sector_size);	if (status < 0) {		mlog_errno(status);		goto bail;	}	hdr = (struct ocfs1_vol_disk_hdr *) (*bh)->b_data;	if (hdr->major_version == OCFS1_MAJOR_VERSION) {		mlog(ML_ERROR, "incompatible version: %u.%u\n",		     hdr->major_version, hdr->minor_version);		status = -EINVAL;	}	if (memcmp(hdr->signature, OCFS1_VOLUME_SIGNATURE,		   strlen(OCFS1_VOLUME_SIGNATURE)) == 0) {		mlog(ML_ERROR, "incompatible volume signature: %8s\n",		     hdr->signature);		status = -EINVAL;	}	brelse(*bh);	*bh = NULL;	if (status < 0) {		mlog(ML_ERROR, "This is an ocfs v1 filesystem which must be "		     "upgraded before mounting with ocfs v2\n");		goto bail;	}	/*	 * Now check at magic offset for 512, 1024, 2048, 4096	 * blocksizes.  4096 is the maximum blocksize because it is	 * the minimum clustersize.	 */	status = -EINVAL;	for (blksize = *sector_size;	     blksize <= OCFS2_MAX_BLOCKSIZE;	     blksize <<= 1) {		tmpstat = ocfs2_get_sector(sb, bh,					   OCFS2_SUPER_BLOCK_BLKNO,					   blksize);		if (tmpstat < 0) {			status = tmpstat;			mlog_errno(status);			goto bail;		}		di = (struct ocfs2_dinode *) (*bh)->b_data;		status = ocfs2_verify_volume(di, *bh, blksize);		if (status >= 0)			goto bail;		brelse(*bh);		*bh = NULL;		if (status != -EAGAIN)			break;	}bail:	return status;}static int ocfs2_verify_heartbeat(struct ocfs2_super *osb){	if (ocfs2_mount_local(osb)) {		if (osb->s_mount_opt & OCFS2_MOUNT_HB_LOCAL) {			mlog(ML_ERROR, "Cannot heartbeat on a locally "			     "mounted device.\n");			return -EINVAL;		}	}	if (!(osb->s_mount_opt & OCFS2_MOUNT_HB_LOCAL)) {		if (!ocfs2_mount_local(osb) && !ocfs2_is_hard_readonly(osb)) {			mlog(ML_ERROR, "Heartbeat has to be started to mount "			     "a read-write clustered device.\n");			return -EINVAL;		}	}	return 0;}static int ocfs2_fill_super(struct super_block *sb, void *data, int silent){	struct dentry *root;	int status, sector_size;	struct mount_options parsed_options;	struct inode *inode = NULL;	struct ocfs2_super *osb = NULL;	struct buffer_head *bh = NULL;	char nodestr[8];	mlog_entry("%p, %p, %i", sb, data, silent);	if (!ocfs2_parse_options(sb, data, &parsed_options, 0)) {		status = -EINVAL;		goto read_super_error;	}	/* for now we only have one cluster/node, make sure we see it	 * in the heartbeat universe */	if (parsed_options.mount_opt & OCFS2_MOUNT_HB_LOCAL) {		if (!o2hb_check_local_node_heartbeating()) {			status = -EINVAL;			goto read_super_error;		}	}	/* probe for superblock */	status = ocfs2_sb_probe(sb, &bh, &sector_size);	if (status < 0) {		mlog(ML_ERROR, "superblock probe failed!\n");		goto read_super_error;	}	status = ocfs2_initialize_super(sb, bh, sector_size);	osb = OCFS2_SB(sb);	if (status < 0) {		mlog_errno(status);		goto read_super_error;	}	brelse(bh);	bh = NULL;	osb->s_mount_opt = parsed_options.mount_opt;	osb->s_atime_quantum = parsed_options.atime_quantum;	osb->preferred_slot = parsed_options.slot;	sb->s_magic = OCFS2_SUPER_MAGIC;	/* Hard readonly mode only if: bdev_read_only, MS_RDONLY,	 * heartbeat=none */	if (bdev_read_only(sb->s_bdev)) {		if (!(sb->s_flags & MS_RDONLY)) {			status = -EACCES;			mlog(ML_ERROR, "Readonly device detected but readonly "

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -