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

📄 namei.c

📁 LINUX1.0内核源代码,学习LINUX编程的一定要看。
💻 C
📖 第 1 页 / 共 2 页
字号:
		}		if (offset < sb->sv_block_size)			continue;		brelse(bh);		bh = NULL;		offset = 0; block++;	}	brelse(bh);	return 1;bad_dir:	brelse(bh);	printk("Bad directory on device %04x\n",inode->i_dev);	return 1;}int sysv_rmdir(struct inode * dir, const char * name, int len){	int retval;	struct inode * inode;	struct buffer_head * bh;	struct sysv_dir_entry * de;	inode = NULL;	bh = sysv_find_entry(dir,name,len,&de);	retval = -ENOENT;	if (!bh)		goto end_rmdir;	retval = -EPERM;	if (!(inode = iget(dir->i_sb, de->inode)))		goto end_rmdir;	if ((dir->i_mode & S_ISVTX) && current->euid &&	    inode->i_uid != current->euid)		goto end_rmdir;	if (inode->i_dev != dir->i_dev)		goto end_rmdir;	if (inode == dir)	/* we may not delete ".", but "../dir" is ok */		goto end_rmdir;	if (!S_ISDIR(inode->i_mode)) {		retval = -ENOTDIR;		goto end_rmdir;	}	if (!empty_dir(inode)) {		retval = -ENOTEMPTY;		goto end_rmdir;	}	if (de->inode != inode->i_ino) {		retval = -ENOENT;		goto end_rmdir;	}	if (inode->i_count > 1) {		retval = -EBUSY;		goto end_rmdir;	}	if (inode->i_nlink != 2)		printk("empty directory has nlink!=2 (%d)\n",inode->i_nlink);	de->inode = 0;	bh->b_dirt = 1;	inode->i_nlink=0;	inode->i_dirt=1;	dir->i_nlink--;	inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;	dir->i_dirt=1;	retval = 0;end_rmdir:	iput(dir);	iput(inode);	brelse(bh);	return retval;}int sysv_unlink(struct inode * dir, const char * name, int len){	int retval;	struct inode * inode;	struct buffer_head * bh;	struct sysv_dir_entry * de;repeat:	retval = -ENOENT;	inode = NULL;	bh = sysv_find_entry(dir,name,len,&de);	if (!bh)		goto end_unlink;	if (!(inode = iget(dir->i_sb, de->inode)))		goto end_unlink;	retval = -EPERM;	if (S_ISDIR(inode->i_mode))		goto end_unlink;	if (de->inode != inode->i_ino) {		iput(inode);		brelse(bh);		current->counter = 0;		schedule();		goto repeat;	}	if ((dir->i_mode & S_ISVTX) && !suser() &&	    current->euid != inode->i_uid &&	    current->euid != dir->i_uid)		goto end_unlink;	if (de->inode != inode->i_ino) {		retval = -ENOENT;		goto end_unlink;	}	if (!inode->i_nlink) {		printk("Deleting nonexistent file (%04x:%lu), %d\n",			inode->i_dev,inode->i_ino,inode->i_nlink);		inode->i_nlink=1;	}	de->inode = 0;	bh->b_dirt = 1;	dir->i_ctime = dir->i_mtime = CURRENT_TIME;	dir->i_dirt = 1;	inode->i_nlink--;	inode->i_ctime = dir->i_ctime;	inode->i_dirt = 1;	retval = 0;end_unlink:	brelse(bh);	iput(inode);	iput(dir);	return retval;}int sysv_symlink(struct inode * dir, const char * name, int len, const char * symname){	struct sysv_dir_entry * de;	struct inode * inode;	struct buffer_head * name_block;	char * name_block_data;	struct super_block * sb;	int i;	char c;	struct buffer_head * bh;	if (!(inode = sysv_new_inode(dir))) {		iput(dir);		return -ENOSPC;	}	inode->i_mode = S_IFLNK | 0777;	inode->i_op = &sysv_symlink_inode_operations;	name_block = sysv_file_bread(inode,0,1,&name_block_data);	if (!name_block) {		iput(dir);		inode->i_nlink--;		inode->i_dirt = 1;		iput(inode);		return -ENOSPC;	}	sb = inode->i_sb;	i = 0;	while (i < sb->sv_block_size_1 && (c = *(symname++)))		name_block_data[i++] = c;	name_block_data[i] = 0;	name_block->b_dirt = 1;	brelse(name_block);	inode->i_size = i;	inode->i_dirt = 1;	bh = sysv_find_entry(dir,name,len,&de);	if (bh) {		inode->i_nlink--;		inode->i_dirt = 1;		iput(inode);		brelse(bh);		iput(dir);		return -EEXIST;	}	i = sysv_add_entry(dir, name, len, &bh, &de);	if (i) {		inode->i_nlink--;		inode->i_dirt = 1;		iput(inode);		iput(dir);		return i;	}	de->inode = inode->i_ino;	bh->b_dirt = 1;	brelse(bh);	iput(dir);	iput(inode);	return 0;}int sysv_link(struct inode * oldinode, struct inode * dir, const char * name, int len){	int error;	struct sysv_dir_entry * de;	struct buffer_head * bh;	if (S_ISDIR(oldinode->i_mode)) {		iput(oldinode);		iput(dir);		return -EPERM;	}	if (oldinode->i_nlink >= oldinode->i_sb->sv_link_max) {		iput(oldinode);		iput(dir);		return -EMLINK;	}	bh = sysv_find_entry(dir,name,len,&de);	if (bh) {		brelse(bh);		iput(dir);		iput(oldinode);		return -EEXIST;	}	error = sysv_add_entry(dir, name, len, &bh, &de);	if (error) {		iput(dir);		iput(oldinode);		return error;	}	de->inode = oldinode->i_ino;	bh->b_dirt = 1;	brelse(bh);	iput(dir);	oldinode->i_nlink++;	oldinode->i_ctime = CURRENT_TIME;	oldinode->i_dirt = 1;	iput(oldinode);	return 0;}/* return 1 if `new' is a subdir of `old' on the same device */static int subdir(struct inode * new_inode, struct inode * old_inode){	int ino;	int result;	new_inode->i_count++;	result = 0;	for (;;) {		if (new_inode == old_inode) {			result = 1;			break;		}		if (new_inode->i_dev != old_inode->i_dev)			break;		ino = new_inode->i_ino;		if (sysv_lookup(new_inode,"..",2,&new_inode))			break;		if (new_inode->i_ino == ino) /* root dir reached ? */			break;	}	iput(new_inode);	return result;}#define PARENT_INO(buffer) \(((struct sysv_dir_entry *) ((buffer) + 1*SYSV_DIRSIZE))->inode)/* * rename uses retrying to avoid race-conditions: at least they should be minimal. * it tries to allocate all the blocks, then sanity-checks, and if the sanity- * checks fail, it tries to restart itself again. Very practical - no changes * are done until we know everything works ok.. and then all the changes can be * done in one fell swoop when we have claimed all the buffers needed. * * Anybody can rename anything with this: the permission checks are left to the * higher-level routines. */static int do_sysv_rename(struct inode * old_dir, const char * old_name, int old_len,	struct inode * new_dir, const char * new_name, int new_len){	struct inode * old_inode, * new_inode;	struct buffer_head * old_bh, * new_bh, * dir_bh;	char * dir_bh_data;	struct sysv_dir_entry * old_de, * new_de;	int retval;	goto start_up;try_again:	brelse(old_bh);	brelse(new_bh);	brelse(dir_bh);	iput(old_inode);	iput(new_inode);	current->counter = 0;	schedule();start_up:	old_inode = new_inode = NULL;	old_bh = new_bh = dir_bh = NULL;	old_bh = sysv_find_entry(old_dir,old_name,old_len,&old_de);	retval = -ENOENT;	if (!old_bh)		goto end_rename;	old_inode = __iget(old_dir->i_sb, old_de->inode, 0); /* don't cross mnt-points */	if (!old_inode)		goto end_rename;	retval = -EPERM;	if ((old_dir->i_mode & S_ISVTX) && 	    current->euid != old_inode->i_uid &&	    current->euid != old_dir->i_uid && !suser())		goto end_rename;	new_bh = sysv_find_entry(new_dir,new_name,new_len,&new_de);	if (new_bh) {		new_inode = __iget(new_dir->i_sb, new_de->inode, 0);		if (!new_inode) {			brelse(new_bh);			new_bh = NULL;		}	}	if (new_inode == old_inode) {		retval = 0;		goto end_rename;	}	if (new_inode && S_ISDIR(new_inode->i_mode)) {		retval = -EISDIR;		if (!S_ISDIR(old_inode->i_mode))			goto end_rename;		retval = -EINVAL;		if (subdir(new_dir, old_inode))			goto end_rename;		retval = -ENOTEMPTY;		if (!empty_dir(new_inode))			goto end_rename;		retval = -EBUSY;		if (new_inode->i_count > 1)			goto end_rename;	}	retval = -EPERM;	if (new_inode && (new_dir->i_mode & S_ISVTX) && 	    current->euid != new_inode->i_uid &&	    current->euid != new_dir->i_uid && !suser())		goto end_rename;	if (S_ISDIR(old_inode->i_mode)) {		retval = -ENOTDIR;		if (new_inode && !S_ISDIR(new_inode->i_mode))			goto end_rename;		retval = -EINVAL;		if (subdir(new_dir, old_inode))			goto end_rename;		retval = -EIO;		dir_bh = sysv_file_bread(old_inode,0,0,&dir_bh_data);		if (!dir_bh)			goto end_rename;		if (PARENT_INO(dir_bh_data) != old_dir->i_ino)			goto end_rename;		retval = -EMLINK;		if (!new_inode && new_dir->i_nlink >= new_dir->i_sb->sv_link_max)			goto end_rename;	}	if (!new_bh) {		retval = sysv_add_entry(new_dir,new_name,new_len,&new_bh,&new_de);		if (retval)			goto end_rename;	}/* sanity checking before doing the rename - avoid races */	if (new_inode && (new_de->inode != new_inode->i_ino))		goto try_again;	if (new_de->inode && !new_inode)		goto try_again;	if (old_de->inode != old_inode->i_ino)		goto try_again;/* ok, that's it */	old_de->inode = 0;	new_de->inode = old_inode->i_ino;	old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;	old_dir->i_dirt = 1;	new_dir->i_ctime = new_dir->i_mtime = CURRENT_TIME;	new_dir->i_dirt = 1;	if (new_inode) {		new_inode->i_nlink--;		new_inode->i_ctime = CURRENT_TIME;		new_inode->i_dirt = 1;	}	old_bh->b_dirt = 1;	new_bh->b_dirt = 1;	if (dir_bh) {		PARENT_INO(dir_bh_data) = new_dir->i_ino;		dir_bh->b_dirt = 1;		old_dir->i_nlink--;		old_dir->i_dirt = 1;		if (new_inode) {			new_inode->i_nlink--;			new_inode->i_dirt = 1;		} else {			new_dir->i_nlink++;			new_dir->i_dirt = 1;		}	}	retval = 0;end_rename:	brelse(dir_bh);	brelse(old_bh);	brelse(new_bh);	iput(old_inode);	iput(new_inode);	iput(old_dir);	iput(new_dir);	return retval;}/* * Ok, rename also locks out other renames, as they can change the parent of * a directory, and we don't want any races. Other races are checked for by * "do_rename()", which restarts if there are inconsistencies. * * Note that there is no race between different filesystems: it's only within * the same device that races occur: many renames can happen at once, as long * as they are on different partitions. */int sysv_rename(struct inode * old_dir, const char * old_name, int old_len,	struct inode * new_dir, const char * new_name, int new_len){	static struct wait_queue * wait = NULL;	static int lock = 0;	int result;	while (lock)		sleep_on(&wait);	lock = 1;	result = do_sysv_rename(old_dir, old_name, old_len,		new_dir, new_name, new_len);	lock = 0;	wake_up(&wait);	return result;}

⌨️ 快捷键说明

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