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

📄 namei.c

📁 《嵌入式系统设计与实例开发实验教材二源码》Linux内核移植与编译实验
💻 C
📖 第 1 页 / 共 3 页
字号:
/* * namei.c * * PURPOSE *      Inode name handling routines for the OSTA-UDF(tm) filesystem. * * CONTACTS *      E-mail regarding any portion of the Linux UDF file system should be *      directed to the development team mailing list (run by majordomo): *              linux_udf@hpesjro.fc.hp.com * * COPYRIGHT *      This file is distributed under the terms of the GNU General Public *      License (GPL). Copies of the GPL can be obtained from: *              ftp://prep.ai.mit.edu/pub/gnu/GPL *      Each contributing author retains all rights to their own work. * *  (C) 1998-2000 Ben Fennema *  (C) 1999-2000 Stelias Computing Inc * * HISTORY * *  12/12/98 blf  Created. Split out the lookup code from dir.c *  04/19/99 blf  link, mknod, symlink support */#include "udfdecl.h"#include "udf_i.h"#include "udf_sb.h"#include <linux/string.h>#include <linux/errno.h>#include <linux/mm.h>#include <linux/slab.h>#include <linux/quotaops.h>#include <linux/locks.h>#include <linux/smp_lock.h>#include <linux/udf_fs.h>static inline int udf_match(int len, const char * const name, struct qstr *qs){	if (len != qs->len)		return 0;	return !memcmp(name, qs->name, len);}int udf_write_fi(struct inode *inode, struct FileIdentDesc *cfi,	struct FileIdentDesc *sfi, struct udf_fileident_bh *fibh,	Uint8 *impuse, Uint8 *fileident){	Uint16 crclen = fibh->eoffset - fibh->soffset - sizeof(tag);	Uint16 crc;	Uint8 checksum = 0;	int i;	int offset;	Uint16 liu = le16_to_cpu(cfi->lengthOfImpUse);	Uint8 lfi = cfi->lengthFileIdent;	int padlen = fibh->eoffset - fibh->soffset - liu - lfi -		sizeof(struct FileIdentDesc);	offset = fibh->soffset + sizeof(struct FileIdentDesc);	if (impuse)	{		if (offset + liu < 0)			memcpy((Uint8 *)sfi->impUse, impuse, liu);		else if (offset >= 0)			memcpy(fibh->ebh->b_data + offset, impuse, liu);		else		{			memcpy((Uint8 *)sfi->impUse, impuse, -offset);			memcpy(fibh->ebh->b_data, impuse - offset, liu + offset);		}	}	offset += liu;	if (fileident)	{		if (offset + lfi < 0)			memcpy((Uint8 *)sfi->fileIdent + liu, fileident, lfi);		else if (offset >= 0)			memcpy(fibh->ebh->b_data + offset, fileident, lfi);		else		{			memcpy((Uint8 *)sfi->fileIdent + liu, fileident, -offset);			memcpy(fibh->ebh->b_data, fileident - offset, lfi + offset);		}	}	offset += lfi;	if (offset + padlen < 0)		memset((Uint8 *)sfi->padding + liu + lfi, 0x00, padlen);	else if (offset >= 0)		memset(fibh->ebh->b_data + offset, 0x00, padlen);	else	{		memset((Uint8 *)sfi->padding + liu + lfi, 0x00, -offset);		memset(fibh->ebh->b_data, 0x00, padlen + offset);	}	crc = udf_crc((Uint8 *)cfi + sizeof(tag), sizeof(struct FileIdentDesc) -		sizeof(tag), 0);	if (fibh->sbh == fibh->ebh)		crc = udf_crc((Uint8 *)sfi->impUse,			crclen + sizeof(tag) - sizeof(struct FileIdentDesc), crc);	else if (sizeof(struct FileIdentDesc) >= -fibh->soffset)		crc = udf_crc(fibh->ebh->b_data + sizeof(struct FileIdentDesc) + fibh->soffset,			crclen + sizeof(tag) - sizeof(struct FileIdentDesc), crc);	else	{		crc = udf_crc((Uint8 *)sfi->impUse,			-fibh->soffset - sizeof(struct FileIdentDesc), crc);		crc = udf_crc(fibh->ebh->b_data, fibh->eoffset, crc);	}	cfi->descTag.descCRC = cpu_to_le32(crc);	cfi->descTag.descCRCLength = cpu_to_le16(crclen);	for (i=0; i<16; i++)		if (i != 4)			checksum += ((Uint8 *)&cfi->descTag)[i];	cfi->descTag.tagChecksum = checksum;	if (sizeof(struct FileIdentDesc) <= -fibh->soffset)		memcpy((Uint8 *)sfi, (Uint8 *)cfi, sizeof(struct FileIdentDesc));	else	{		memcpy((Uint8 *)sfi, (Uint8 *)cfi, -fibh->soffset);		memcpy(fibh->ebh->b_data, (Uint8 *)cfi - fibh->soffset,			sizeof(struct FileIdentDesc) + fibh->soffset);	}	if (fibh->sbh != fibh->ebh)		mark_buffer_dirty_inode(fibh->ebh, inode);	mark_buffer_dirty_inode(fibh->sbh, inode);	return 0;}static struct FileIdentDesc *udf_find_entry(struct inode *dir, struct dentry *dentry,	struct udf_fileident_bh *fibh,	struct FileIdentDesc *cfi){	struct FileIdentDesc *fi=NULL;	loff_t f_pos;	int block, flen;	char fname[255];	char *nameptr;	Uint8 lfi;	Uint16 liu;	loff_t size = (udf_ext0_offset(dir) + dir->i_size) >> 2;	lb_addr bloc, eloc;	Uint32 extoffset, elen, offset;	struct buffer_head *bh = NULL;	if (!dir)		return NULL;	f_pos = (udf_ext0_offset(dir) >> 2);	fibh->soffset = fibh->eoffset = (f_pos & ((dir->i_sb->s_blocksize - 1) >> 2)) << 2;	if (inode_bmap(dir, f_pos >> (dir->i_sb->s_blocksize_bits - 2),		&bloc, &extoffset, &eloc, &elen, &offset, &bh) == EXTENT_RECORDED_ALLOCATED)	{		offset >>= dir->i_sb->s_blocksize_bits;		block = udf_get_lb_pblock(dir->i_sb, eloc, offset);		if ((++offset << dir->i_sb->s_blocksize_bits) < elen)		{			if (UDF_I_ALLOCTYPE(dir) == ICB_FLAG_AD_SHORT)				extoffset -= sizeof(short_ad);			else if (UDF_I_ALLOCTYPE(dir) == ICB_FLAG_AD_LONG)				extoffset -= sizeof(long_ad);		}		else			offset = 0;	}	else	{		udf_release_data(bh);		return NULL;	}	if (!(fibh->sbh = fibh->ebh = udf_tread(dir->i_sb, block)))	{		udf_release_data(bh);		return NULL;	}	while ( (f_pos < size) )	{		fi = udf_fileident_read(dir, &f_pos, fibh, cfi, &bloc, &extoffset, &eloc, &elen, &offset, &bh);		if (!fi)		{			if (fibh->sbh != fibh->ebh)				udf_release_data(fibh->ebh);			udf_release_data(fibh->sbh);			udf_release_data(bh);			return NULL;		}		liu = le16_to_cpu(cfi->lengthOfImpUse);		lfi = cfi->lengthFileIdent;		if (fibh->sbh == fibh->ebh)		{			nameptr = fi->fileIdent + liu;		}		else		{			int poffset;	/* Unpaded ending offset */			poffset = fibh->soffset + sizeof(struct FileIdentDesc) + liu + lfi;			if (poffset >= lfi)				nameptr = (Uint8 *)(fibh->ebh->b_data + poffset - lfi);			else			{				nameptr = fname;				memcpy(nameptr, fi->fileIdent + liu, lfi - poffset);				memcpy(nameptr + lfi - poffset, fibh->ebh->b_data, poffset);			}		}		if ( (cfi->fileCharacteristics & FILE_DELETED) != 0 )		{			if ( !UDF_QUERY_FLAG(dir->i_sb, UDF_FLAG_UNDELETE) )				continue;		}	    		if ( (cfi->fileCharacteristics & FILE_HIDDEN) != 0 )		{			if ( !UDF_QUERY_FLAG(dir->i_sb, UDF_FLAG_UNHIDE) )				continue;		}		if (!lfi)			continue;		if ((flen = udf_get_filename(dir->i_sb, nameptr, fname, lfi)))		{			if (udf_match(flen, fname, &(dentry->d_name)))			{				udf_release_data(bh);				return fi;			}		}	}	if (fibh->sbh != fibh->ebh)		udf_release_data(fibh->ebh);	udf_release_data(fibh->sbh);	udf_release_data(bh);	return NULL;}/* * udf_lookup * * PURPOSE *	Look-up the inode for a given name. * * DESCRIPTION *	Required - lookup_dentry() will return -ENOTDIR if this routine is not *	available for a directory. The filesystem is useless if this routine is *	not available for at least the filesystem's root directory. * *	This routine is passed an incomplete dentry - it must be completed by *	calling d_add(dentry, inode). If the name does not exist, then the *	specified inode must be set to null. An error should only be returned *	when the lookup fails for a reason other than the name not existing. *	Note that the directory inode semaphore is held during the call. * *	Refer to lookup_dentry() in fs/namei.c *	lookup_dentry() -> lookup() -> real_lookup() -> . * * PRE-CONDITIONS *	dir			Pointer to inode of parent directory. *	dentry			Pointer to dentry to complete. * * POST-CONDITIONS *	<return>		Zero on success. * * HISTORY *	July 1, 1997 - Andrew E. Mileski *	Written, tested, and released. */static struct dentry *udf_lookup(struct inode *dir, struct dentry *dentry){	struct inode *inode = NULL;	struct FileIdentDesc cfi, *fi;	struct udf_fileident_bh fibh;	if (dentry->d_name.len > UDF_NAME_LEN)		return ERR_PTR(-ENAMETOOLONG);#ifdef UDF_RECOVERY	/* temporary shorthand for specifying files by inode number */	if (!strncmp(dentry->d_name.name, ".B=", 3) )	{		lb_addr lb = { 0, simple_strtoul(dentry->d_name.name+3, NULL, 0) };		inode = udf_iget(dir->i_sb, lb);		if (!inode)			return ERR_PTR(-EACCES);	}	else#endif /* UDF_RECOVERY */	if ((fi = udf_find_entry(dir, dentry, &fibh, &cfi)))	{		if (fibh.sbh != fibh.ebh)			udf_release_data(fibh.ebh);		udf_release_data(fibh.sbh);		inode = udf_iget(dir->i_sb, lelb_to_cpu(cfi.icb.extLocation));		if ( !inode )			return ERR_PTR(-EACCES);	}	d_add(dentry, inode);	return NULL;}static struct FileIdentDesc *udf_add_entry(struct inode *dir, struct dentry *dentry,	struct udf_fileident_bh *fibh,	struct FileIdentDesc *cfi, int *err){	struct super_block *sb;	struct FileIdentDesc *fi=NULL;	struct ustr unifilename;	char name[UDF_NAME_LEN], fname[UDF_NAME_LEN];	int namelen;	loff_t f_pos;	int flen;	char *nameptr;	loff_t size = (udf_ext0_offset(dir) + dir->i_size) >> 2;	int nfidlen;	Uint8 lfi;	Uint16 liu;	int block;	lb_addr bloc, eloc;	Uint32 extoffset, elen, offset;	struct buffer_head *bh = NULL;	sb = dir->i_sb;	if (dentry)	{		if (!dentry->d_name.len)		{			*err = -EINVAL;			return NULL;		}		if ( !(udf_char_to_ustr(&unifilename, dentry->d_name.name, dentry->d_name.len)) )		{			*err = -ENAMETOOLONG;			return NULL;		}		if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8))		{			if ( !(namelen = udf_UTF8toCS0(name, &unifilename, UDF_NAME_LEN)) )			{				*err = -ENAMETOOLONG;				return NULL;			}		}		else if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))		{			if ( !(namelen = udf_NLStoCS0(UDF_SB(sb)->s_nls_map, name, &unifilename, UDF_NAME_LEN)) )			{				*err = -ENAMETOOLONG;				return NULL;			}		}		else			return NULL;	}	else		namelen = 0;	nfidlen = (sizeof(struct FileIdentDesc) + namelen + 3) & ~3;	f_pos = (udf_ext0_offset(dir) >> 2);	fibh->soffset = fibh->eoffset = (f_pos & ((dir->i_sb->s_blocksize - 1) >> 2)) << 2;	if (inode_bmap(dir, f_pos >> (dir->i_sb->s_blocksize_bits - 2),		&bloc, &extoffset, &eloc, &elen, &offset, &bh) == EXTENT_RECORDED_ALLOCATED)	{		offset >>= dir->i_sb->s_blocksize_bits;		block = udf_get_lb_pblock(dir->i_sb, eloc, offset);		if ((++offset << dir->i_sb->s_blocksize_bits) < elen)		{			if (UDF_I_ALLOCTYPE(dir) == ICB_FLAG_AD_SHORT)				extoffset -= sizeof(short_ad);			else if (UDF_I_ALLOCTYPE(dir) == ICB_FLAG_AD_LONG)				extoffset -= sizeof(long_ad);		}		else			offset = 0;		if (!(fibh->sbh = fibh->ebh = udf_tread(dir->i_sb, block)))		{			udf_release_data(bh);			*err = -EIO;			return NULL;		}			block = UDF_I_LOCATION(dir).logicalBlockNum;			while ( (f_pos < size) )		{			fi = udf_fileident_read(dir, &f_pos, fibh, cfi, &bloc, &extoffset, &eloc, &elen, &offset, &bh);				if (!fi)			{				if (fibh->sbh != fibh->ebh)					udf_release_data(fibh->ebh);				udf_release_data(fibh->sbh);				udf_release_data(bh);				*err = -EIO;				return NULL;			}				liu = le16_to_cpu(cfi->lengthOfImpUse);			lfi = cfi->lengthFileIdent;				if (fibh->sbh == fibh->ebh)

⌨️ 快捷键说明

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