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

📄 inode.c

📁 LINUX 1.0 内核c源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/*
 *  linux/fs/sysv/inode.c
 *
 *  minix/inode.c
 *  Copyright (C) 1991, 1992  Linus Torvalds
 *
 *  xenix/inode.c
 *  Copyright (C) 1992  Doug Evans
 *
 *  coh/inode.c
 *  Copyright (C) 1993  Pascal Haible, Bruno Haible
 *
 *  sysv/inode.c
 *  Copyright (C) 1993  Paul B. Monday
 *
 *  sysv/inode.c
 *  Copyright (C) 1993  Bruno Haible
 *
 *  This file contains code for allocating/freeing inodes and for read/writing
 *  the superblock.
 */

#include <linux/sched.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/sysv_fs.h>
#include <linux/stat.h>
#include <linux/string.h>
#include <linux/locks.h>

#include <asm/segment.h>

void _coh_wait_on_inode (struct inode * inode)
{
	struct wait_queue wait = { current, NULL };

	add_wait_queue(&inode->u.sysv_i.i_wait, &wait);
repeat:
	current->state = TASK_UNINTERRUPTIBLE;
	if (inode->u.sysv_i.i_lock) {
		schedule();
		goto repeat;
	}
	remove_wait_queue(&inode->u.sysv_i.i_wait, &wait);
	current->state = TASK_RUNNING;
}

void sysv_put_inode(struct inode *inode)
{
	if (inode->i_nlink)
		return;
	inode->i_size = 0;
	sysv_truncate(inode);
	sysv_free_inode(inode);
}


static struct super_operations sysv_sops = { 
	sysv_read_inode,
	sysv_notify_change,
	sysv_write_inode,
	sysv_put_inode,
	sysv_put_super,
	sysv_write_super,
	sysv_statfs,
	NULL
};

/* The following functions try to recognize specific filesystems.
 * We recognize:
 * - Xenix FS by its magic number.
 * - SystemV FS by its magic number.
 * - Coherent FS by its funny fname/fpack field.
 * We discriminate among SystemV4 and SystemV2 FS by the assumption that
 * the time stamp is not < 01-01-1980.
 */

static void detected_bs512 (struct super_block *sb)
{
	sb->sv_block_size = 512;
	sb->sv_block_size_1 = 512-1;
	sb->sv_block_size_bits = 9;
	sb->sv_block_size_ratio = 2;
	sb->sv_block_size_ratio_1 = 2-1;
	sb->sv_block_size_ratio_bits = 1;
	sb->sv_inodes_per_block = 512/64;
	sb->sv_inodes_per_block_1 = 512/64-1;
	sb->sv_inodes_per_block_bits = 9-6;
	sb->sv_toobig_block = 10 + 
	  (sb->sv_ind_per_block = 512/4) +
	  (sb->sv_ind_per_block_2 = (512/4)*(512/4)) +
	  (sb->sv_ind_per_block_3 = (512/4)*(512/4)*(512/4));
	sb->sv_ind_per_block_1 = 512/4-1;
	sb->sv_ind_per_block_2_1 = (512/4)*(512/4)-1;
	sb->sv_ind_per_block_2_bits = 2 *
	  (sb->sv_ind_per_block_bits = 9-2);
}

static void detected_bs1024 (struct super_block *sb)
{
	sb->sv_block_size = 1024;
	sb->sv_block_size_1 = 1024-1;
	sb->sv_block_size_bits = 10;
	sb->sv_block_size_ratio = 1;
	sb->sv_block_size_ratio_1 = 1-1;
	sb->sv_block_size_ratio_bits = 0;
	sb->sv_inodes_per_block = 1024/64;
	sb->sv_inodes_per_block_1 = 1024/64-1;
	sb->sv_inodes_per_block_bits = 10-6;
	sb->sv_toobig_block = 10 + 
	  (sb->sv_ind_per_block = 1024/4) +
	  (sb->sv_ind_per_block_2 = (1024/4)*(1024/4)) +
	  (sb->sv_ind_per_block_3 = (1024/4)*(1024/4)*(1024/4));
	sb->sv_ind_per_block_1 = 1024/4-1;
	sb->sv_ind_per_block_2_1 = (1024/4)*(1024/4)-1;
	sb->sv_ind_per_block_2_bits = 2 *
	  (sb->sv_ind_per_block_bits = 10-2);
}

static const char* detect_xenix (struct super_block *sb, struct buffer_head *bh)
{
	struct xenix_super_block * sbd;

	sbd = (struct xenix_super_block *) bh->b_data;
	if (sbd->s_magic != 0x2b5544)
		return NULL;
	sb->sv_type = FSTYPE_XENIX;
	sb->sv_convert = 0;
	sb->sv_kludge_symlinks = 1;
	sb->sv_truncate = 1;
	sb->sv_link_max = XENIX_LINK_MAX;
	sb->sv_fic_size = XENIX_NICINOD;
	sb->sv_flc_size = XENIX_NICFREE;
	sb->sv_bh = bh;
	sb->sv_sbd = (char *) sbd;
	sb->sv_sb_fic_count = &sbd->s_ninode;
	sb->sv_sb_fic_inodes = &sbd->s_inode[0];
	sb->sv_sb_total_free_inodes = &sbd->s_tinode;
	sb->sv_sb_flc_count = &sbd->s_nfree;
	sb->sv_sb_flc_blocks = &sbd->s_free[0];
	sb->sv_sb_total_free_blocks = &sbd->s_tfree;
	sb->sv_sb_time = &sbd->s_time;
	sb->sv_block_base = 0;
	sb->sv_firstinodezone = 2;
	sb->sv_firstdatazone = sbd->s_isize;
	sb->sv_nzones = sbd->s_fsize;
	sb->sv_ndatazones = sb->sv_nzones - sb->sv_firstdatazone;
	switch (sbd->s_type) {
		case 1: detected_bs512(sb); break;
		case 2: detected_bs1024(sb); break;
		default: return NULL;
	}
	return "Xenix";
}

static const char* detect_sysv4 (struct super_block *sb, struct buffer_head *bh)
{
	struct sysv4_super_block * sbd;

	sbd = (struct sysv4_super_block *) (bh->b_data + BLOCK_SIZE/2);
	if (sbd->s_magic != 0xfd187e20)
		return NULL;
	if (sbd->s_time < 315532800) /* this is likely to happen on SystemV2 FS */
		return NULL;
	sb->sv_type = FSTYPE_SYSV4;
	sb->sv_convert = 0;
	sb->sv_kludge_symlinks = 0; /* ?? */
	sb->sv_truncate = 1;
	sb->sv_link_max = SYSV_LINK_MAX;
	sb->sv_fic_size = SYSV_NICINOD;
	sb->sv_flc_size = SYSV_NICFREE;
	sb->sv_bh = bh;
	sb->sv_sbd = (char *) sbd;
	sb->sv_sb_fic_count = &sbd->s_ninode;
	sb->sv_sb_fic_inodes = &sbd->s_inode[0];
	sb->sv_sb_total_free_inodes = &sbd->s_tinode;
	sb->sv_sb_flc_count = &sbd->s_nfree;
	sb->sv_sb_flc_blocks = &sbd->s_free[0];
	sb->sv_sb_total_free_blocks = &sbd->s_tfree;
	sb->sv_sb_time = &sbd->s_time;
	sb->sv_block_base = 0;
	sb->sv_firstinodezone = 2;
	sb->sv_firstdatazone = sbd->s_isize;
	sb->sv_nzones = sbd->s_fsize;
	sb->sv_ndatazones = sb->sv_nzones - sb->sv_firstdatazone;
	switch (sbd->s_type) {
		case 1: detected_bs512(sb); break;
		case 2: detected_bs1024(sb); break;
		default: return NULL;
	}
	return "SystemV";
}

static const char* detect_sysv2 (struct super_block *sb, struct buffer_head *bh)
{
	struct sysv2_super_block * sbd;

	sbd = (struct sysv2_super_block *) (bh->b_data + BLOCK_SIZE/2);
	if (sbd->s_magic != 0xfd187e20)
		return NULL;
	if (sbd->s_time < 315532800) /* this is likely to happen on SystemV4 FS */
		return NULL;
	sb->sv_type = FSTYPE_SYSV2;
	sb->sv_convert = 0;
	sb->sv_kludge_symlinks = 0; /* ?? */
	sb->sv_truncate = 1;
	sb->sv_link_max = SYSV_LINK_MAX;
	sb->sv_fic_size = SYSV_NICINOD;
	sb->sv_flc_size = SYSV_NICFREE;
	sb->sv_bh = bh;
	sb->sv_sbd = (char *) sbd;
	sb->sv_sb_fic_count = &sbd->s_ninode;
	sb->sv_sb_fic_inodes = &sbd->s_inode[0];
	sb->sv_sb_total_free_inodes = &sbd->s_tinode;
	sb->sv_sb_flc_count = &sbd->s_nfree;
	sb->sv_sb_flc_blocks = &sbd->s_free[0];
	sb->sv_sb_total_free_blocks = &sbd->s_tfree;
	sb->sv_sb_time = &sbd->s_time;
	sb->sv_block_base = 0;
	sb->sv_firstinodezone = 2;
	sb->sv_firstdatazone = sbd->s_isize;
	sb->sv_nzones = sbd->s_fsize;
	sb->sv_ndatazones = sb->sv_nzones - sb->sv_firstdatazone;
	switch (sbd->s_type) {
		case 1: detected_bs512(sb); break;
		case 2: detected_bs1024(sb); break;
		default: return NULL;
	}
	return "SystemV Release 2";
}

static const char* detect_coherent (struct super_block *sb, struct buffer_head *bh)
{
	struct coh_super_block * sbd;

	sbd = (struct coh_super_block *) (bh->b_data + BLOCK_SIZE/2);
	if ((memcmp(sbd->s_fname,"noname",6) && memcmp(sbd->s_fname,"xxxxx ",6))
	    || (memcmp(sbd->s_fpack,"nopack",6) && memcmp(sbd->s_fpack,"xxxxx\n",6)))
		return NULL;
	sb->sv_type = FSTYPE_COH;
	sb->sv_convert = 1;
	sb->sv_kludge_symlinks = 1;
	sb->sv_truncate = 1;
	sb->sv_link_max = COH_LINK_MAX;
	sb->sv_fic_size = COH_NICINOD;
	sb->sv_flc_size = COH_NICFREE;
	sb->sv_bh = bh;
	sb->sv_sbd = (char *) sbd;
	sb->sv_sb_fic_count = &sbd->s_ninode;
	sb->sv_sb_fic_inodes = &sbd->s_inode[0];
	sb->sv_sb_total_free_inodes = &sbd->s_tinode;
	sb->sv_sb_flc_count = &sbd->s_nfree;
	sb->sv_sb_flc_blocks = &sbd->s_free[0];
	sb->sv_sb_total_free_blocks = &sbd->s_tfree;
	sb->sv_sb_time = &sbd->s_time;
	sb->sv_block_base = 0;
	sb->sv_firstinodezone = 2;
	sb->sv_firstdatazone = sbd->s_isize;
	sb->sv_nzones = from_coh_ulong(sbd->s_fsize);
	sb->sv_ndatazones = sb->sv_nzones - sb->sv_firstdatazone;
	detected_bs512(sb);
	return "Coherent";
}

struct super_block *sysv_read_super(struct super_block *sb,void *data, 
				     int silent)
{
	struct buffer_head *bh;
	const char *found;
	int dev = sb->s_dev;

	if (1024 != sizeof (struct xenix_super_block))
		panic("Xenix FS: bad super-block size");
	if ((512 != sizeof (struct sysv4_super_block))
            || (512 != sizeof (struct sysv2_super_block)))
		panic("SystemV FS: bad super-block size");
	if (500 != sizeof (struct coh_super_block))
		panic("Coherent FS: bad super-block size");
	if (64 != sizeof (struct sysv_inode))
		panic("sysv fs: bad i-node size");
	lock_super(sb);
	sb->s_blocksize = BLOCK_SIZE; /* anything else not supported by the block device drivers */
	sb->s_blocksize_bits = BLOCK_SIZE_BITS;

	/* Try to read Xenix superblock */
	if ((bh = bread(dev, 1, BLOCK_SIZE)) != NULL) {
		if ((found = detect_xenix(sb,bh)) != NULL)
			goto ok;
		brelse(bh);
	}
	if ((bh = bread(dev, 0, BLOCK_SIZE)) != NULL) {
		/* Try to recognize SystemV superblock */
		if ((found = detect_sysv4(sb,bh)) != NULL)
			goto ok;
		if ((found = detect_sysv2(sb,bh)) != NULL)
			goto ok;
		/* Try to recognize Coherent superblock */
		if ((found = detect_coherent(sb,bh)) != NULL)
			goto ok;
		brelse(bh);
	}
	/* Try to recognize SystemV2 superblock */
	/* Offset by 1 track, i.e. most probably 9, 15, or 18 kilobytes. */
	{	static int offsets[] = { 9, 15, 18, };
		int i;
		for (i = 0; i < sizeof(offsets)/sizeof(offsets[0]); i++)
			if ((bh = bread(dev, offsets[i], BLOCK_SIZE)) != NULL) {
				/* Try to recognize SystemV superblock */
				if ((found = detect_sysv2(sb,bh)) != NULL) {
					sb->sv_block_base = offsets[i];
					goto ok;
				}
				brelse(bh);
			}
	}
	sb->s_dev=0;
	unlock_super(sb);
	if (!silent)
		printk("VFS: unable to read Xenix/SystemV/Coherent superblock on device %d/%d\n",MAJOR(dev),MINOR(dev));
	return NULL;

	ok:
	sb->sv_ninodes = (sb->sv_firstdatazone - sb->sv_firstinodezone) << sb->sv_inodes_per_block_bits;
	if (!silent)
		printk("VFS: Found a %s FS (block size = %d) on device %d/%d\n",found,sb->sv_block_size,MAJOR(dev),MINOR(dev));
	sb->s_magic = SYSV_MAGIC_BASE + sb->sv_type;
	/* set up enough so that it can read an inode */
	sb->s_dev = dev;
	sb->s_op = &sysv_sops;
	sb->s_mounted = iget(sb,SYSV_ROOT_INO);
	unlock_super(sb);
	if (!sb->s_mounted) {
		brelse(bh);
		sb->s_dev = 0;
		printk("SysV FS: get root inode failed\n");
		return NULL;
	}
	sb->s_dirt = 1; /* brelse(bh); occurs when the disk is unmounted. */
	return sb;
}

/* This is only called on sync() and umount(), when s_dirt=1. */
void sysv_write_super (struct super_block *sb)
{
	lock_super(sb);
	if (sb->sv_bh->b_dirt) {
		/* If we are going to write out the super block,
		   then attach current time stamp. */
		unsigned long time = CURRENT_TIME;
		if (sb->sv_convert)
			time = to_coh_ulong(time);
		*sb->sv_sb_time = time;
	}
	sb->s_dirt = 0;
	unlock_super(sb);
}

void sysv_put_super(struct super_block *sb)
{
	/* we can assume sysv_write_super() has already been called */
	lock_super(sb);
	brelse(sb->sv_bh);
	sb->s_dev = 0;
	unlock_super(sb);
}

void sysv_statfs(struct super_block *sb, struct statfs *buf)
{
	long tmp;

	put_fs_long(sb->s_magic, &buf->f_type);		/* type of filesystem */
	put_fs_long(sb->sv_block_size, &buf->f_bsize);	/* block size */
	put_fs_long(sb->sv_ndatazones, &buf->f_blocks);	/* total data blocks in file system */
	tmp = sysv_count_free_blocks(sb);
	put_fs_long(tmp, &buf->f_bfree);		/* free blocks in fs */
	put_fs_long(tmp, &buf->f_bavail);		/* free blocks available to non-superuser */
	put_fs_long(sb->sv_ninodes, &buf->f_files);	/* total file nodes in file system */
	put_fs_long(sysv_count_free_inodes(sb), &buf->f_ffree);	/* free file nodes in fs */
	put_fs_long(SYSV_NAMELEN, &buf->f_namelen);
	/* Don't know what value to put in buf->f_fsid */	/* file system id */
}


/* bmap support for running executables and shared libraries.
   Used only if block_size = BLOCK_SIZE. */

#define IND_PER_BLOCK	(BLOCK_SIZE / sizeof(sysv_zone_t))

static inline int inode_bmap(struct super_block * sb, struct inode * inode, int nr)
{
	int tmp = inode->u.sysv_i.i_data[nr];
	if (!tmp)
		return 0;
	return tmp + sb->sv_block_base;
}

static int block_bmap(struct super_block * sb, struct buffer_head * bh, int nr, int convert)
{
	int tmp;

	if (!bh)
		return 0;
	tmp = ((sysv_zone_t *) bh->b_data) [nr];
	if (convert)
		tmp = from_coh_ulong(tmp);

⌨️ 快捷键说明

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