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

📄 namei.c.bak

📁 一份精简的linux内核源代码
💻 BAK
📖 第 1 页 / 共 2 页
字号:
/* *  linux/fs/namei.c * *  (C) 1991  Linus Torvalds *//* * Some corrections by tytso. */#include <linux/sched.h>#include <linux/kernel.h>#include <asm/segment.h>#include <string.h>#include <fcntl.h>#include <errno.h>#include <const.h>#include <sys/stat.h>#define ACC_MODE(x) ("\004\002\006\377"[(x)&O_ACCMODE])/*根据x获得读写权限(x=0->004,x=1->002....);O_ACCMODE=000011为屏蔽码;/002等代表一个字符*//* * comment out this line if you want names > NAME_LEN chars to be * truncated. Else they will be disallowed. *//* #define NO_TRUNCATE */#define MAY_EXEC 1	/* 可执行 */#define MAY_WRITE 2/* 可写 */#define MAY_READ 4/* 可读 *//* *	permission() * * is used to check for read/write/execute permissions on a file. * I don't know if we should look at just the euid or both euid and * uid, but that should be easily changed. */static int permission(struct m_inode * inode,int mask)/* 检查文件访问许可权,mask为属性屏蔽码,返回1则访问许可*/{	int mode = inode->i_mode;/* special case: not even root can read/write a deleted file */	if (inode->i_dev && !inode->i_nlinks)/* i节点有对应设备,但引用计数为0,表示该文件已被删除 */		return 0;	else if (current->euid==inode->i_uid)/* 进程的有效用户id(euid)与i节点的用户id相同,则取文件宿主的访问权限 */		mode >>= 6;	else if (current->egid==inode->i_gid)/*进程的有效组id(egid)与i节点的组id相同,则取组用户的访问权限 */		mode >>= 3;	if (((mode & mask & 0007) == mask) || suser())/* 若所取的访问权限与屏蔽码相同或是超级用户则返回1 */		return 1;	return 0;}/* * ok, we cannot use strncmp, as the name is not in our data space. * Thus we'll have to use match. No big problem. Match also makes * some sanity tests. * * NOTE! unlike strncmp, match returns 1 for success, 0 for failure. */static int match(int len,const char * name,struct dir_entry * de)/*目录项中的文件名同指定的文件名比较 */{	register int same __asm__("ax");	if (!de || !de->inode || len > NAME_LEN)		return 0;	if (len < NAME_LEN && de->name[len])		return 0;	__asm__("cld\n\t"		"fs ; repe ; cmpsb\n\t"/*在用户空间进行循环比较[edi][esi]指向的字符 */		"setz %%al"		:"=a" (same)		:"0" (0),"S" ((long) name),"D" ((long) de->name),"c" (len)		:"cx","di","si");	return same;}/* *	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. * * This also takes care of the few special cases due to '..'-traversal * over a pseudo-root and a mount point. */static struct buffer_head * find_entry(struct m_inode ** dir,/* 在指定目录中寻找一个与名字匹配的目录项,返回一个含有目录相的高速缓冲块及目录项本身 */	const char * name, int namelen, struct dir_entry ** res_dir)/*该函数并不读取目录项i节点 */{	int entries;	int block,i;	struct buffer_head * bh;	struct dir_entry * de;	struct super_block * sb;#ifdef NO_TRUNCATE /* 当文件名长度过长时是否截断文件名 */	if (namelen > NAME_LEN)		return NULL;#else	if (namelen > NAME_LEN)		namelen = NAME_LEN;#endif	entries = (*dir)->i_size / (sizeof (struct dir_entry));/* 获得目录中含有的项数(目录的i_size中存放着此目录文件的数据长度) */	*res_dir = NULL;	if (!namelen)		return NULL;/* check for '..', as we might have to do some "magic" for it */	if (namelen==2 && get_fs_byte(name)=='.' && get_fs_byte(name+1)=='.') {/* '..' in a pseudo-root results in a faked '.' (just change namelen) */		if ((*dir) == current->root)/*若当前进程的根目录为dir,则不能访问dir的父目录 */			namelen=1;		else if ((*dir)->i_num == ROOT_INO) {/*若目录为文件系统的根目录,则dir=文件系统被安装到的i节点*//* '..' over a mount-point results in 'dir' being exchanged for the mounted   directory-inode. NOTE! We set mounted, so that we can iput the new dir */			sb=get_super((*dir)->i_dev);			if (sb->s_imount) {				iput(*dir);				(*dir)=sb->s_imount;				(*dir)->i_count++;			}		}	}	if (!(block = (*dir)->i_zone[0]))		return NULL;	if (!(bh = bread((*dir)->i_dev,block)))		return NULL;	i = 0;	de = (struct dir_entry *) bh->b_data;	while (i < entries) {		if ((char *)de >= BLOCK_SIZE+bh->b_data) {			brelse(bh);			bh = NULL;			if (!(block = bmap(*dir,i/DIR_ENTRIES_PER_BLOCK)) ||			    !(bh = bread((*dir)->i_dev,block))) {				i += DIR_ENTRIES_PER_BLOCK;				continue;			}			de = (struct dir_entry *) bh->b_data;		}		if (match(namelen,name,de)) {			*res_dir = de;			return bh;		}		de++;		i++;	}	brelse(bh);	return NULL;}/* *	add_entry() * * adds a file entry to the specified directory, using the same * semantics as 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. */static struct buffer_head * add_entry(struct m_inode * dir,/*向指定的目录中添加一个目录项(只添加了目录项的名字,未添加该名字对应的i节点) */	const char * name, int namelen, struct dir_entry ** res_dir)/*读入一块并寻找空项,找到则插入,未找到则读入下一块(若该块为空则新建),若无法在插入到中间则插入到在最后块的最后目录项 */{                                                              	int block,i;	struct buffer_head * bh;	struct dir_entry * de;	*res_dir = NULL;#ifdef NO_TRUNCATE	if (namelen > NAME_LEN)		return NULL;#else	if (namelen > NAME_LEN)		namelen = NAME_LEN;#endif	if (!namelen)		return NULL;	if (!(block = dir->i_zone[0]))		return NULL;	if (!(bh = bread(dir->i_dev,block)))		return NULL;	i = 0;	de = (struct dir_entry *) bh->b_data;	while (1) {		if ((char *)de >= BLOCK_SIZE+bh->b_data) {			brelse(bh);			bh = NULL;			block = create_block(dir,i/DIR_ENTRIES_PER_BLOCK);			if (!block)				return NULL;			if (!(bh = bread(dir->i_dev,block))) {				i += DIR_ENTRIES_PER_BLOCK;				continue;			}			de = (struct dir_entry *) bh->b_data;		}		if (i*sizeof(struct dir_entry) >= dir->i_size) {			de->inode=0;			dir->i_size = (i+1)*sizeof(struct dir_entry);			dir->i_dirt = 1;			dir->i_ctime = CURRENT_TIME;		}		if (!de->inode) {			dir->i_mtime = CURRENT_TIME;			for (i=0; i < NAME_LEN ; i++)				de->name[i]=(i<namelen)?get_fs_byte(name+i):0;			bh->b_dirt = 1;			*res_dir = de;			return bh;		}		de++;		i++;	}	brelse(bh);	return NULL;}/* *	get_dir() * * Getdir traverses the pathname until it hits the topmost directory. * It returns NULL on failure. */static struct m_inode * get_dir(const char * pathname)/*搜索指定路径名的末端目录i节点(最后一个“/”之前的目录i节点)并返回该i节点 */{	char c;	const char * thisname;	struct m_inode * inode;	struct buffer_head * bh;	int namelen,inr,idev;	struct dir_entry * de;	if (!current->root || !current->root->i_count)/*当前进程根目录或工作目录为空或空闲,则出错 */		panic("No root inode");	if (!current->pwd || !current->pwd->i_count)		panic("No cwd inode");	if ((c=get_fs_byte(pathname))=='/') {/*若路径名的首字符为“/”则 路径名为绝对路径名,从进程根目录开始搜索*/		inode = current->root;		pathname++;	} else if (c) /*若路径名的首字符非“/”则 路径名为相对路径名,从进程工作目录开始搜索*/		inode = current->pwd;	else		return NULL;	/* empty name is bad */	inode->i_count++;	while (1) {		thisname = pathname;		if (!S_ISDIR(inode->i_mode) || !permission(inode,MAY_EXEC)) {			iput(inode);			return NULL;		}		for(namelen=0;(c=get_fs_byte(pathname++))&&(c!='/');namelen++)			/* nothing */ ;		if (!c)			return inode;		if (!(bh = find_entry(&inode,thisname,namelen,&de))) {			iput(inode);			return NULL;		}		inr = de->inode;		idev = inode->i_dev;		brelse(bh);		iput(inode);		if (!(inode = iget(idev,inr)))			return NULL;	}}/* *	dir_namei() * * dir_namei() returns the inode of the directory of the * specified name, and the name within that directory. */static struct m_inode * dir_namei(const char * pathname,/*返回指定路径下的末端目录i节点及最后一个“/”后的字符 */	int * namelen, const char ** name){	char c;	const char * basename;	struct m_inode * dir;	if (!(dir = get_dir(pathname)))		return NULL;	basename = pathname;	while (c=get_fs_byte(pathname++))		if (c=='/')			basename=pathname;	*namelen = pathname-basename-1;	*name = basename;	return dir;}/* *	namei() * * is used by most simple commands to get the inode of a specified name. * Open, link etc use their own routines, but this is enough for things * like 'chmod' etc. */struct m_inode * namei(const char * pathname)/*搜索指定路径名的末端i节点并返回该i节点 */{	const char * basename;	int inr,dev,namelen;	struct m_inode * dir;	struct buffer_head * bh;	struct dir_entry * de;	if (!(dir = dir_namei(pathname,&namelen,&basename)))		return NULL;	if (!namelen)			/* special case: '/usr/' etc */		return dir;	bh = find_entry(&dir,basename,namelen,&de);	if (!bh) {		iput(dir);		return NULL;	}	inr = de->inode;	dev = dir->i_dev;	brelse(bh);	iput(dir);	dir=iget(dev,inr);	if (dir) {		dir->i_atime=CURRENT_TIME;		dir->i_dirt=1;	}	return dir;}/* *	open_namei() * * namei for open - this is in fact almost the whole open-routine. */int open_namei(const char * pathname, int flag, int mode,/*打开指定路径的文件(实质是从设备上读入该文件的i节点,并返回该i节点的指针)*/	struct m_inode ** res_inode){	const char * basename;	int inr,dev,namelen;	struct m_inode * dir, *inode;	struct buffer_head * bh;	struct dir_entry * de;	if ((flag & O_TRUNC) && !(flag & O_ACCMODE))/*标志的初始化 */		flag |= O_WRONLY;	mode &= 0777 & ~current->umask;	mode |= I_REGULAR;	if (!(dir = dir_namei(pathname,&namelen,&basename)))/* 获取指路径的i节点,若路径名以"/"结尾,则返回目录i节点(若对目录执行非法操作如读写则出错退出) */		return -ENOENT;	if (!namelen) {			/* special case: '/usr/' etc */ 		if (!(flag & (O_ACCMODE|O_CREAT|O_TRUNC))) {			*res_inode=dir;			return 0;		}		iput(dir);		return -EISDIR;	}	bh = find_entry(&dir,basename,namelen,&de);	if (!bh) {/*创建文件(末端文件不存在(bh==nulll),且创建文件标志置位) */		if (!(flag & O_CREAT)) {			iput(dir);			return -ENOENT;		}		if (!permission(dir,MAY_WRITE)) {/*创建文件时还需判断当前进程是否有写权限*/			iput(dir);			return -EACCES;		}		inode = new_inode(dir->i_dev);/*创建文件 */		if (!inode) {			iput(dir);			return -ENOSPC;		}		inode->i_uid = current->euid;		inode->i_mode = mode;		inode->i_dirt = 1;		bh = add_entry(dir,basename,namelen,&de);		if (!bh) {			inode->i_nlinks--;			iput(inode);			iput(dir);			return -ENOSPC;		}		de->inode = inode->i_num;		bh->b_dirt = 1;		brelse(bh);		iput(dir);		*res_inode = inode;		return 0;

⌨️ 快捷键说明

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