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

📄 namei.c

📁 elinux jffs初始版本 具体了解JFFS的文件系统!
💻 C
📖 第 1 页 / 共 2 页
字号:
	    strcmp (".", de->name) || strcmp ("..", de1->name)) {	    	ext2_warning (inode->i_sb, "empty_dir",			      "bad directory (dir #%lu) - no `.' or `..'",			      inode->i_ino);		brelse (bh);		return 1;	}	offset = de->rec_len + de1->rec_len;	de = (struct ext2_dir_entry *) ((char *) de1 + de1->rec_len);	while (offset < inode->i_size ) {		if (!bh || (void *) de >= (void *) (bh->b_data + sb->s_blocksize)) {			brelse (bh);			bh = ext2_bread (inode, offset >> EXT2_BLOCK_SIZE_BITS(sb), 1, &err);			if (!bh) {				ext2_error (sb, "empty_dir",					    "directory #%lu contains a hole at offset %lu",					    inode->i_ino, offset);				offset += sb->s_blocksize;				continue;			}			de = (struct ext2_dir_entry *) bh->b_data;		}		if (!ext2_check_dir_entry ("empty_dir", inode, de, bh,					   offset)) {			brelse (bh);			return 1;		}		if (de->inode) {			brelse (bh);			return 0;		}		offset += de->rec_len;		de = (struct ext2_dir_entry *) ((char *) de + de->rec_len);	}	brelse (bh);	return 1;}int ext2_rmdir (struct inode * dir, const char * name, int len){	int retval;	struct inode * inode;	struct buffer_head * bh;	struct ext2_dir_entry * de;repeat:	if (!dir)		return -ENOENT;	inode = NULL;	if (len > EXT2_NAME_LEN) {		iput (dir);		return -ENAMETOOLONG;	}	bh = ext2_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 (inode->i_sb->dq_op)		inode->i_sb->dq_op->initialize (inode, -1);	if (inode->i_dev != dir->i_dev) {		retval = -EBUSY;		goto end_rmdir;	}	if (de->inode != inode->i_ino) {		iput(inode);		brelse(bh);		current->counter = 0;		schedule();		goto repeat;	}        if ((dir->i_mode & S_ISVTX) && !fsuser() &&            current->fsuid != inode->i_uid &&            current->fsuid != dir->i_uid)		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;	}	down(&inode->i_sem);	if (!empty_dir (inode))		retval = -ENOTEMPTY;	else if (de->inode != inode->i_ino)		retval = -ENOENT;	else {		if (inode->i_count > 1) {		/*		 * Are we deleting the last instance of a busy directory?		 * Better clean up if so.		 *		 * Make directory empty (it will be truncated when finally		 * dereferenced).  This also inhibits ext2_add_entry.		 */			inode->i_size = 0;		}		retval = ext2_delete_entry (de, bh);		dir->i_version = ++event;	}	up(&inode->i_sem);	if (retval)		goto end_rmdir;	mark_buffer_dirty(bh, 1);	if (IS_SYNC(dir)) {		ll_rw_block (WRITE, 1, &bh);		wait_on_buffer (bh);	}	if (inode->i_nlink != 2)		ext2_warning (inode->i_sb, "ext2_rmdir",			      "empty directory has nlink!=2 (%d)",			      inode->i_nlink);	inode->i_version = ++event;	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;end_rmdir:	iput (dir);	iput (inode);	brelse (bh);	return retval;}int ext2_unlink (struct inode * dir, const char * name, int len){	int retval;	struct inode * inode;	struct buffer_head * bh;	struct ext2_dir_entry * de;repeat:	if (!dir)		return -ENOENT;	retval = -ENOENT;	inode = NULL;	if (len > EXT2_NAME_LEN) {		iput (dir);		return -ENAMETOOLONG;	}	bh = ext2_find_entry (dir, name, len, &de);	if (!bh)		goto end_unlink;	if (!(inode = iget (dir->i_sb, de->inode)))		goto end_unlink;	if (inode->i_sb->dq_op)		inode->i_sb->dq_op->initialize (inode, -1);	retval = -EPERM;	if (S_ISDIR(inode->i_mode))		goto end_unlink;	if (IS_APPEND(inode) || IS_IMMUTABLE(inode))		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) && !fsuser() &&	    current->fsuid != inode->i_uid &&	    current->fsuid != dir->i_uid)		goto end_unlink;	if (!inode->i_nlink) {		ext2_warning (inode->i_sb, "ext2_unlink",			      "Deleting nonexistent file (%lu), %d",			      inode->i_ino, inode->i_nlink);		inode->i_nlink = 1;	}	retval = ext2_delete_entry (de, bh);	if (retval)		goto end_unlink;	dir->i_version = ++event;	mark_buffer_dirty(bh, 1);	if (IS_SYNC(dir)) {		ll_rw_block (WRITE, 1, &bh);		wait_on_buffer (bh);	}	dir->i_ctime = dir->i_mtime = CURRENT_TIME;	dir->i_dirt = 1;	inode->i_nlink--;	inode->i_dirt = 1;	inode->i_ctime = dir->i_ctime;	retval = 0;end_unlink:	brelse (bh);	iput (inode);	iput (dir);	return retval;}int ext2_symlink (struct inode * dir, const char * name, int len,		  const char * symname){	struct ext2_dir_entry * de;	struct inode * inode = NULL;	struct buffer_head * bh = NULL, * name_block = NULL;	char * link;	int i, err;	int l;	char c;	if (!(inode = ext2_new_inode (dir, S_IFLNK, &err))) {		iput (dir);		return err;	}	inode->i_mode = S_IFLNK | S_IRWXUGO;	inode->i_op = &ext2_symlink_inode_operations;	for (l = 0; l < inode->i_sb->s_blocksize - 1 &&	     symname [l]; l++)		;	if (l >= sizeof (inode->u.ext2_i.i_data)) {		ext2_debug ("l=%d, normal symlink\n", l);		name_block = ext2_bread (inode, 0, 1, &err);		if (!name_block) {			iput (dir);			inode->i_nlink--;			inode->i_dirt = 1;			iput (inode);			return err;		}		link = name_block->b_data;	} else {		link = (char *) inode->u.ext2_i.i_data;		ext2_debug ("l=%d, fast symlink\n", l);	}	i = 0;	while (i < inode->i_sb->s_blocksize - 1 && (c = *(symname++)))		link[i++] = c;	link[i] = 0;	if (name_block) {		mark_buffer_dirty(name_block, 1);		brelse (name_block);	}	inode->i_size = i;	inode->i_dirt = 1;	bh = ext2_find_entry (dir, name, len, &de);	if (bh) {		inode->i_nlink--;		inode->i_dirt = 1;		iput (inode);		brelse (bh);		iput (dir);		return -EEXIST;	}	bh = ext2_add_entry (dir, name, len, &de, &err);	if (!bh) {		inode->i_nlink--;		inode->i_dirt = 1;		iput (inode);		iput (dir);		return err;	}	de->inode = inode->i_ino;	dir->i_version = ++event;	dcache_add(dir, de->name, de->name_len, de->inode);	mark_buffer_dirty(bh, 1);	if (IS_SYNC(dir)) {		ll_rw_block (WRITE, 1, &bh);		wait_on_buffer (bh);	}	brelse (bh);	iput (dir);	iput (inode);	return 0;}int ext2_link (struct inode * oldinode, struct inode * dir,	       const char * name, int len){	struct ext2_dir_entry * de;	struct buffer_head * bh;	int err;	if (S_ISDIR(oldinode->i_mode)) {		iput (oldinode);		iput (dir);		return -EPERM;	}	if (IS_APPEND(oldinode) || IS_IMMUTABLE(oldinode)) {		iput (oldinode);		iput (dir);		return -EPERM;	}	if (oldinode->i_nlink >= EXT2_LINK_MAX) {		iput (oldinode);		iput (dir);		return -EMLINK;	}	bh = ext2_find_entry (dir, name, len, &de);	if (bh) {		brelse (bh);		iput (dir);		iput (oldinode);		return -EEXIST;	}	bh = ext2_add_entry (dir, name, len, &de, &err);	if (!bh) {		iput (dir);		iput (oldinode);		return err;	}	de->inode = oldinode->i_ino;	dir->i_version = ++event;	dcache_add(dir, de->name, de->name_len, de->inode);	mark_buffer_dirty(bh, 1);	if (IS_SYNC(dir)) {		ll_rw_block (WRITE, 1, &bh);		wait_on_buffer (bh);	}	brelse (bh);	iput (dir);	oldinode->i_nlink++;	oldinode->i_ctime = CURRENT_TIME;	oldinode->i_dirt = 1;	iput (oldinode);	return 0;}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 (ext2_lookup (new_inode, "..", 2, &new_inode))			break;		if (new_inode->i_ino == ino)			break;	}	iput (new_inode);	return result;}#define PARENT_INO(buffer) \	((struct ext2_dir_entry *) ((char *) buffer + \	((struct ext2_dir_entry *) buffer)->rec_len))->inode#define PARENT_NAME(buffer) \	((struct ext2_dir_entry *) ((char *) buffer + \	((struct ext2_dir_entry *) buffer)->rec_len))->name/* * 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_ext2_rename (struct inode * old_dir, const char * old_name,			   int old_len, struct inode * new_dir,			   const char * new_name, int new_len,			   int must_be_dir){	struct inode * old_inode, * new_inode;	struct buffer_head * old_bh, * new_bh, * dir_bh;	struct ext2_dir_entry * old_de, * new_de;	int retval;	goto start_up;try_again:	if (new_bh && new_de) {		ext2_delete_entry(new_de, new_bh);		new_dir->i_version = ++event;	}	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;	new_de = NULL;	retval = -ENAMETOOLONG;	if (old_len > EXT2_NAME_LEN)		goto end_rename;	old_bh = ext2_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;	if (must_be_dir && !S_ISDIR(old_inode->i_mode))		goto end_rename;	retval = -EPERM;	if ((old_dir->i_mode & S_ISVTX) && 	    current->fsuid != old_inode->i_uid &&	    current->fsuid != old_dir->i_uid && !fsuser())		goto end_rename;	if (IS_APPEND(old_inode) || IS_IMMUTABLE(old_inode))		goto end_rename;	new_bh = ext2_find_entry (new_dir, new_name, new_len, &new_de);	if (new_bh) {		new_inode = __iget (new_dir->i_sb, new_de->inode, 0); /* no mntp cross */		if (!new_inode) {			brelse (new_bh);			new_bh = NULL;		} else {			if (new_inode->i_sb->dq_op)				new_inode->i_sb->dq_op->initialize (new_inode, -1);		}	}	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) {		if ((new_dir->i_mode & S_ISVTX) &&		    current->fsuid != new_inode->i_uid &&		    current->fsuid != new_dir->i_uid && !fsuser())			goto end_rename;		if (IS_APPEND(new_inode) || IS_IMMUTABLE(new_inode))			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;		dir_bh = ext2_bread (old_inode, 0, 0, &retval);		if (!dir_bh)			goto end_rename;		if (PARENT_INO(dir_bh->b_data) != old_dir->i_ino)			goto end_rename;		retval = -EMLINK;		if (!new_inode && new_dir->i_nlink >= EXT2_LINK_MAX)			goto end_rename;	}	if (!new_bh)		new_bh = ext2_add_entry (new_dir, new_name, new_len, &new_de,					 &retval);	if (!new_bh)		goto end_rename;	new_dir->i_version = ++event;	/*	 * 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	 */	new_de->inode = old_inode->i_ino;	dcache_add(new_dir, new_de->name, new_de->name_len, new_de->inode);	retval = ext2_delete_entry (old_de, old_bh);	if (retval == -ENOENT)		goto try_again;	if (retval)		goto end_rename;	old_dir->i_version = ++event;	if (new_inode) {		new_inode->i_nlink--;		new_inode->i_ctime = CURRENT_TIME;		new_inode->i_dirt = 1;	}	old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;	old_dir->i_dirt = 1;	if (dir_bh) {		PARENT_INO(dir_bh->b_data) = new_dir->i_ino;		dcache_add(old_inode, "..", 2, new_dir->i_ino);		mark_buffer_dirty(dir_bh, 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;		}	}	mark_buffer_dirty(old_bh,  1);	if (IS_SYNC(old_dir)) {		ll_rw_block (WRITE, 1, &old_bh);		wait_on_buffer (old_bh);	}	mark_buffer_dirty(new_bh, 1);	if (IS_SYNC(new_dir)) {		ll_rw_block (WRITE, 1, &new_bh);		wait_on_buffer (new_bh);	}	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. * * In the second extended file system, we use a lock flag stored in the memory * super-block.  This way, we really lock other renames only if they occur * on the same file system */int ext2_rename (struct inode * old_dir, const char * old_name, int old_len,		 struct inode * new_dir, const char * new_name, int new_len,		 int must_be_dir){	int result;	while (old_dir->i_sb->u.ext2_sb.s_rename_lock)		sleep_on (&old_dir->i_sb->u.ext2_sb.s_rename_wait);	old_dir->i_sb->u.ext2_sb.s_rename_lock = 1;	result = do_ext2_rename (old_dir, old_name, old_len, new_dir,				 new_name, new_len, must_be_dir);	old_dir->i_sb->u.ext2_sb.s_rename_lock = 0;	wake_up (&old_dir->i_sb->u.ext2_sb.s_rename_wait);	return result;}

⌨️ 快捷键说明

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