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

📄 namei.c

📁 嵌入式系统设计与实例开发实验教材二源码 多线程应用程序设计 串行端口程序设计 AD接口实验 CAN总线通信实验 GPS通信实验 Linux内核移植与编译实验 IC卡读写实验 SD驱动使
💻 C
📖 第 1 页 / 共 2 页
字号:
/* *  linux/fs/ext3/namei.c * * Copyright (C) 1992, 1993, 1994, 1995 * Remy Card (card@masi.ibp.fr) * Laboratoire MASI - Institut Blaise Pascal * Universite Pierre et Marie Curie (Paris VI) * *  from * *  linux/fs/minix/namei.c * *  Copyright (C) 1991, 1992  Linus Torvalds * *  Big-endian to little-endian byte-swapping/bitmaps by *        David S. Miller (davem@caip.rutgers.edu), 1995 *  Directory entry file type support and forward compatibility hooks *  	for B-tree directories by Theodore Ts'o (tytso@mit.edu), 1998 */#include <linux/fs.h>#include <linux/jbd.h>#include <linux/sched.h>#include <linux/ext3_fs.h>#include <linux/ext3_jbd.h>#include <linux/fcntl.h>#include <linux/stat.h>#include <linux/string.h>#include <linux/locks.h>#include <linux/quotaops.h>/* * define how far ahead to read directories while searching them. */#define NAMEI_RA_CHUNKS  2#define NAMEI_RA_BLOCKS  4#define NAMEI_RA_SIZE        (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)#define NAMEI_RA_INDEX(c,b)  (((c) * NAMEI_RA_BLOCKS) + (b))/* * NOTE! unlike strncmp, ext3_match returns 1 for success, 0 for failure. * * `len <= EXT3_NAME_LEN' is guaranteed by caller. * `de != NULL' is guaranteed by caller. */static inline int ext3_match (int len, const char * const name,			      struct ext3_dir_entry_2 * de){	if (len != de->name_len)		return 0;	if (!de->inode)		return 0;	return !memcmp(name, de->name, len);}/* * Returns 0 if not found, -1 on failure, and 1 on success */static int inline search_dirblock(struct buffer_head * bh,				  struct inode *dir,				  struct dentry *dentry,				  unsigned long offset,				  struct ext3_dir_entry_2 ** res_dir){	struct ext3_dir_entry_2 * de;	char * dlimit;	int de_len;	const char *name = dentry->d_name.name;	int namelen = dentry->d_name.len;	de = (struct ext3_dir_entry_2 *) bh->b_data;	dlimit = bh->b_data + dir->i_sb->s_blocksize;	while ((char *) de < dlimit) {		/* this code is executed quadratically often */		/* do minimal checking `by hand' */		if ((char *) de + namelen <= dlimit &&		    ext3_match (namelen, name, de)) {			/* found a match - just to be sure, do a full check */			if (!ext3_check_dir_entry("ext3_find_entry",						  dir, de, bh, offset))				return -1;			*res_dir = de;			return 1;		}		/* prevent looping on a bad block */		de_len = le16_to_cpu(de->rec_len);		if (de_len <= 0)			return -1;		offset += de_len;		de = (struct ext3_dir_entry_2 *) ((char *) de + de_len);	}	return 0;}/* *	ext3_find_entry() * * finds an entry in the specified directory with the wanted name. It * returns the cache buffer in which the entry was found, and the entry * itself (as a parameter - res_dir). It does NOT read the inode of the * entry - you'll have to do that yourself if you want to. * * The returned buffer_head has ->b_count elevated.  The caller is expected * to brelse() it when appropriate. */static struct buffer_head * ext3_find_entry (struct dentry *dentry,					struct ext3_dir_entry_2 ** res_dir){	struct super_block * sb;	struct buffer_head * bh_use[NAMEI_RA_SIZE];	struct buffer_head * bh, *ret = NULL;	unsigned long start, block, b;	int ra_max = 0;		/* Number of bh's in the readahead				   buffer, bh_use[] */	int ra_ptr = 0;		/* Current index into readahead				   buffer */	int num = 0;	int nblocks, i, err;	struct inode *dir = dentry->d_parent->d_inode;	*res_dir = NULL;	sb = dir->i_sb;	nblocks = dir->i_size >> EXT3_BLOCK_SIZE_BITS(sb);	start = dir->u.ext3_i.i_dir_start_lookup;	if (start >= nblocks)		start = 0;	block = start;restart:	do {		/*		 * We deal with the read-ahead logic here.		 */		if (ra_ptr >= ra_max) {			/* Refill the readahead buffer */			ra_ptr = 0;			b = block;			for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {				/*				 * Terminate if we reach the end of the				 * directory and must wrap, or if our				 * search has finished at this block.				 */				if (b >= nblocks || (num && block == start)) {					bh_use[ra_max] = NULL;					break;				}				num++;				bh = ext3_getblk(NULL, dir, b++, 0, &err);				bh_use[ra_max] = bh;				if (bh)					ll_rw_block(READ, 1, &bh);			}		}		if ((bh = bh_use[ra_ptr++]) == NULL)			goto next;		wait_on_buffer(bh);		if (!buffer_uptodate(bh)) {			/* read error, skip block & hope for the best */			brelse(bh);			goto next;		}		i = search_dirblock(bh, dir, dentry,			    block << EXT3_BLOCK_SIZE_BITS(sb), res_dir);		if (i == 1) {			dir->u.ext3_i.i_dir_start_lookup = block;			ret = bh;			goto cleanup_and_exit;		} else {			brelse(bh);			if (i < 0)				goto cleanup_and_exit;		}	next:		if (++block >= nblocks)			block = 0;	} while (block != start);	/*	 * If the directory has grown while we were searching, then	 * search the last part of the directory before giving up.	 */	block = nblocks;	nblocks = dir->i_size >> EXT3_BLOCK_SIZE_BITS(sb);	if (block < nblocks) {		start = 0;		goto restart;	}		cleanup_and_exit:	/* Clean up the read-ahead blocks */	for (; ra_ptr < ra_max; ra_ptr++)		brelse (bh_use[ra_ptr]);	return ret;}static struct dentry *ext3_lookup(struct inode * dir, struct dentry *dentry){	struct inode * inode;	struct ext3_dir_entry_2 * de;	struct buffer_head * bh;	if (dentry->d_name.len > EXT3_NAME_LEN)		return ERR_PTR(-ENAMETOOLONG);	bh = ext3_find_entry(dentry, &de);	inode = NULL;	if (bh) {		unsigned long ino = le32_to_cpu(de->inode);		brelse (bh);		inode = iget(dir->i_sb, ino);		if (!inode)			return ERR_PTR(-EACCES);	}	d_add(dentry, inode);	return NULL;}#define S_SHIFT 12static unsigned char ext3_type_by_mode[S_IFMT >> S_SHIFT] = {	[S_IFREG >> S_SHIFT]	EXT3_FT_REG_FILE,	[S_IFDIR >> S_SHIFT]	EXT3_FT_DIR,	[S_IFCHR >> S_SHIFT]	EXT3_FT_CHRDEV,	[S_IFBLK >> S_SHIFT]	EXT3_FT_BLKDEV,	[S_IFIFO >> S_SHIFT]	EXT3_FT_FIFO,	[S_IFSOCK >> S_SHIFT]	EXT3_FT_SOCK,	[S_IFLNK >> S_SHIFT]	EXT3_FT_SYMLINK,};static inline void ext3_set_de_type(struct super_block *sb,				struct ext3_dir_entry_2 *de,				umode_t mode) {	if (EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_FILETYPE))		de->file_type = ext3_type_by_mode[(mode & S_IFMT)>>S_SHIFT];}/* *	ext3_add_entry() * * adds a file entry to the specified directory, using the same * semantics as ext3_find_entry(). It returns NULL if it failed. * * NOTE!! The inode part of 'de' is left at 0 - which means you * may not sleep between calling this and putting something into * the entry, as someone else might have used it while you slept. *//* * AKPM: the journalling code here looks wrong on the error paths */static int ext3_add_entry (handle_t *handle, struct dentry *dentry,	struct inode *inode){	struct inode *dir = dentry->d_parent->d_inode;	const char *name = dentry->d_name.name;	int namelen = dentry->d_name.len;	unsigned long offset;	unsigned short rec_len;	struct buffer_head * bh;	struct ext3_dir_entry_2 * de, * de1;	struct super_block * sb;	int	retval;	sb = dir->i_sb;	if (!namelen)		return -EINVAL;	bh = ext3_bread (handle, dir, 0, 0, &retval);	if (!bh)		return retval;	rec_len = EXT3_DIR_REC_LEN(namelen);	offset = 0;	de = (struct ext3_dir_entry_2 *) bh->b_data;	while (1) {		if ((char *)de >= sb->s_blocksize + bh->b_data) {			brelse (bh);			bh = NULL;			bh = ext3_bread (handle, dir,				offset >> EXT3_BLOCK_SIZE_BITS(sb), 1, &retval);			if (!bh)				return retval;			if (dir->i_size <= offset) {				if (dir->i_size == 0) {					brelse(bh);					return -ENOENT;				}				ext3_debug ("creating next block\n");				BUFFER_TRACE(bh, "get_write_access");				ext3_journal_get_write_access(handle, bh);				de = (struct ext3_dir_entry_2 *) bh->b_data;				de->inode = 0;				de->rec_len = le16_to_cpu(sb->s_blocksize);				dir->u.ext3_i.i_disksize =					dir->i_size = offset + sb->s_blocksize;				dir->u.ext3_i.i_flags &= ~EXT3_INDEX_FL;				ext3_mark_inode_dirty(handle, dir);			} else {				ext3_debug ("skipping to next block\n");				de = (struct ext3_dir_entry_2 *) bh->b_data;			}		}		if (!ext3_check_dir_entry ("ext3_add_entry", dir, de, bh,					   offset)) {			brelse (bh);			return -ENOENT;		}		if (ext3_match (namelen, name, de)) {				brelse (bh);				return -EEXIST;		}		if ((le32_to_cpu(de->inode) == 0 &&				le16_to_cpu(de->rec_len) >= rec_len) ||		    (le16_to_cpu(de->rec_len) >=				EXT3_DIR_REC_LEN(de->name_len) + rec_len)) {			BUFFER_TRACE(bh, "get_write_access");			ext3_journal_get_write_access(handle, bh);			/* By now the buffer is marked for journaling */			offset += le16_to_cpu(de->rec_len);			if (le32_to_cpu(de->inode)) {				de1 = (struct ext3_dir_entry_2 *) ((char *) de +					EXT3_DIR_REC_LEN(de->name_len));				de1->rec_len =					cpu_to_le16(le16_to_cpu(de->rec_len) -					EXT3_DIR_REC_LEN(de->name_len));				de->rec_len = cpu_to_le16(						EXT3_DIR_REC_LEN(de->name_len));				de = de1;			}			de->file_type = EXT3_FT_UNKNOWN;			if (inode) {				de->inode = cpu_to_le32(inode->i_ino);				ext3_set_de_type(dir->i_sb, de, inode->i_mode);			} else				de->inode = 0;			de->name_len = namelen;			memcpy (de->name, name, namelen);			/*			 * XXX shouldn't update any times until successful			 * completion of syscall, but too many callers depend			 * on this.			 *			 * XXX similarly, too many callers depend on			 * ext3_new_inode() setting the times, but error			 * recovery deletes the inode, so the worst that can			 * happen is that the times are slightly out of date			 * and/or different from the directory change time.			 */			dir->i_mtime = dir->i_ctime = CURRENT_TIME;			dir->u.ext3_i.i_flags &= ~EXT3_INDEX_FL;			ext3_mark_inode_dirty(handle, dir);			dir->i_version = ++event;			BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");			ext3_journal_dirty_metadata(handle, bh);			brelse(bh);			return 0;		}		offset += le16_to_cpu(de->rec_len);		de = (struct ext3_dir_entry_2 *)			((char *) de + le16_to_cpu(de->rec_len));	}	brelse (bh);	return -ENOSPC;}/* * ext3_delete_entry deletes a directory entry by merging it with the * previous entry */static int ext3_delete_entry (handle_t *handle, 			      struct inode * dir,			      struct ext3_dir_entry_2 * de_del,			      struct buffer_head * bh){	struct ext3_dir_entry_2 * de, * pde;	int i;	i = 0;	pde = NULL;	de = (struct ext3_dir_entry_2 *) bh->b_data;	while (i < bh->b_size) {		if (!ext3_check_dir_entry("ext3_delete_entry", dir, de, bh, i))			return -EIO;		if (de == de_del)  {			BUFFER_TRACE(bh, "get_write_access");			ext3_journal_get_write_access(handle, bh);			if (pde)				pde->rec_len =					cpu_to_le16(le16_to_cpu(pde->rec_len) +						    le16_to_cpu(de->rec_len));			else				de->inode = 0;			dir->i_version = ++event;			BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");			ext3_journal_dirty_metadata(handle, bh);			return 0;		}		i += le16_to_cpu(de->rec_len);		pde = de;		de = (struct ext3_dir_entry_2 *)			((char *) de + le16_to_cpu(de->rec_len));	}	return -ENOENT;}/* * ext3_mark_inode_dirty is somewhat expensive, so unlike ext2 we * do not perform it in these functions.  We perform it at the call site, * if it is needed. */static inline void ext3_inc_count(handle_t *handle, struct inode *inode){	inode->i_nlink++;}static inline void ext3_dec_count(handle_t *handle, struct inode *inode){	inode->i_nlink--;}static int ext3_add_nondir(handle_t *handle,		struct dentry *dentry, struct inode *inode){	int err = ext3_add_entry(handle, dentry, inode);	if (!err) {		d_instantiate(dentry, inode);		return 0;	}	ext3_dec_count(handle, inode);	iput(inode);	return err;}/* * By the time this is called, we already have created * the directory cache entry for the new file, but it * is so far negative - it has no inode. * * If the create succeeds, we fill in the inode information * with d_instantiate().  */static int ext3_create (struct inode * dir, struct dentry * dentry, int mode){	handle_t *handle; 	struct inode * inode;	int err;	handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS + 3);	if (IS_ERR(handle))		return PTR_ERR(handle);	if (IS_SYNC(dir))		handle->h_sync = 1;	inode = ext3_new_inode (handle, dir, mode);	err = PTR_ERR(inode);	if (!IS_ERR(inode)) {		inode->i_op = &ext3_file_inode_operations;		inode->i_fop = &ext3_file_operations;		inode->i_mapping->a_ops = &ext3_aops;		err = ext3_add_nondir(handle, dentry, inode);		ext3_mark_inode_dirty(handle, inode);	}	ext3_journal_stop(handle, dir);	return err;}static int ext3_mknod (struct inode * dir, struct dentry *dentry,			int mode, int rdev){	handle_t *handle;	struct inode *inode;	int err;	handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS + 3);	if (IS_ERR(handle))		return PTR_ERR(handle);	if (IS_SYNC(dir))		handle->h_sync = 1;	inode = ext3_new_inode (handle, dir, mode);	err = PTR_ERR(inode);	if (!IS_ERR(inode)) {		init_special_inode(inode, mode, rdev);		err = ext3_add_nondir(handle, dentry, inode);		ext3_mark_inode_dirty(handle, inode);	}	ext3_journal_stop(handle, dir);	return err;}static int ext3_mkdir(struct inode * dir, struct dentry * dentry, int mode){	handle_t *handle;	struct inode * inode;	struct buffer_head * dir_block;	struct ext3_dir_entry_2 * de;	int err;	if (dir->i_nlink >= EXT3_LINK_MAX)		return -EMLINK;	handle = ext3_journal_start(dir, EXT3_DATA_TRANS_BLOCKS + 3);	if (IS_ERR(handle))		return PTR_ERR(handle);	if (IS_SYNC(dir))		handle->h_sync = 1;	inode = ext3_new_inode (handle, dir, S_IFDIR);	err = PTR_ERR(inode);	if (IS_ERR(inode))		goto out_stop;	inode->i_op = &ext3_dir_inode_operations;	inode->i_fop = &ext3_dir_operations;	inode->i_size = inode->u.ext3_i.i_disksize = inode->i_sb->s_blocksize;	inode->i_blocks = 0;		dir_block = ext3_bread (handle, inode, 0, 1, &err);	if (!dir_block) {		inode->i_nlink--; /* is this nlink == 0? */		ext3_mark_inode_dirty(handle, inode);		iput (inode);		goto out_stop;	}	BUFFER_TRACE(dir_block, "get_write_access");	ext3_journal_get_write_access(handle, dir_block);	de = (struct ext3_dir_entry_2 *) dir_block->b_data;	de->inode = cpu_to_le32(inode->i_ino);	de->name_len = 1;	de->rec_len = cpu_to_le16(EXT3_DIR_REC_LEN(de->name_len));	strcpy (de->name, ".");	ext3_set_de_type(dir->i_sb, de, S_IFDIR);	de = (struct ext3_dir_entry_2 *)			((char *) de + le16_to_cpu(de->rec_len));	de->inode = cpu_to_le32(dir->i_ino);	de->rec_len = cpu_to_le16(inode->i_sb->s_blocksize-EXT3_DIR_REC_LEN(1));	de->name_len = 2;	strcpy (de->name, "..");	ext3_set_de_type(dir->i_sb, de, S_IFDIR);	inode->i_nlink = 2;	BUFFER_TRACE(dir_block, "call ext3_journal_dirty_metadata");	ext3_journal_dirty_metadata(handle, dir_block);	brelse (dir_block);	inode->i_mode = S_IFDIR | mode;	if (dir->i_mode & S_ISGID)		inode->i_mode |= S_ISGID;	ext3_mark_inode_dirty(handle, inode);	err = ext3_add_entry (handle, dentry, inode);	if (err)		goto out_no_entry;	dir->i_nlink++;	dir->u.ext3_i.i_flags &= ~EXT3_INDEX_FL;	ext3_mark_inode_dirty(handle, dir);	d_instantiate(dentry, inode);out_stop:

⌨️ 快捷键说明

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