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

📄 namei.c

📁 Linux内核源代码 为压缩文件 是<<Linux内核>>一书中的源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
/* *  linux/fs/vfat/namei.c * *  Written 1992,1993 by Werner Almesberger * *  Windows95/Windows NT compatible extended MSDOS filesystem *    by Gordon Chaffee Copyright (C) 1995.  Send bug reports for the *    VFAT filesystem to <chaffee@cs.berkeley.edu>.  Specify *    what file operation caused you trouble and if you can duplicate *    the problem, send a script that demonstrates it. * *  Short name translation 1999 by Wolfram Pienkoss <wp@bszh.de> */#define __NO_VERSION__#include <linux/module.h>#include <linux/sched.h>#include <linux/msdos_fs.h>#include <linux/nls.h>#include <linux/kernel.h>#include <linux/errno.h>#include <linux/string.h>#include <linux/ctype.h>#include <linux/stat.h>#include <linux/mm.h>#include <linux/malloc.h>#include "../fat/msbuffer.h"#define DEBUG_LEVEL 0#if (DEBUG_LEVEL >= 1)#  define PRINTK1(x) printk x#else#  define PRINTK1(x)#endif#if (DEBUG_LEVEL >= 2)#  define PRINTK2(x) printk x#else#  define PRINTK2(x)#endif#if (DEBUG_LEVEL >= 3)#  define PRINTK3(x) printk x#else#  define PRINTK3(x)#endif#ifndef DEBUG# define CHECK_STACK#else# define CHECK_STACK check_stack(__FILE__, __LINE__)#endifstatic int vfat_hashi(struct dentry *parent, struct qstr *qstr);static int vfat_hash(struct dentry *parent, struct qstr *qstr);static int vfat_cmpi(struct dentry *dentry, struct qstr *a, struct qstr *b);static int vfat_cmp(struct dentry *dentry, struct qstr *a, struct qstr *b);static int vfat_revalidate(struct dentry *dentry, int);static struct dentry_operations vfat_dentry_ops[4] = {	{		d_hash:		vfat_hashi,		d_compare:	vfat_cmpi,	},	{		d_revalidate:	vfat_revalidate,		d_hash:		vfat_hashi,		d_compare:	vfat_cmpi,	},	{		d_hash:		vfat_hash,		d_compare:	vfat_cmp,	},	{		d_revalidate:	vfat_revalidate,		d_hash:		vfat_hash,		d_compare:	vfat_cmp,	}};static int vfat_revalidate(struct dentry *dentry, int flags){	PRINTK1(("vfat_revalidate: %s\n", dentry->d_name.name));	spin_lock(&dcache_lock);	if (dentry->d_time == dentry->d_parent->d_inode->i_version) {		spin_unlock(&dcache_lock);		return 1;	}	spin_unlock(&dcache_lock);	return 0;}static int simple_getbool(char *s, int *setval){	if (s) {		if (!strcmp(s,"1") || !strcmp(s,"yes") || !strcmp(s,"true")) {			*setval = 1;		} else if (!strcmp(s,"0") || !strcmp(s,"no") || !strcmp(s,"false")) {			*setval = 0;		} else {			return 0;		}	} else {		*setval = 1;	}	return 1;}static int parse_options(char *options,	struct fat_mount_options *opts){	char *this_char,*value,save,*savep;	int ret, val;	opts->unicode_xlate = opts->posixfs = 0;	opts->numtail = 1;	opts->utf8 = 0;	if (!options) return 1;	save = 0;	savep = NULL;	ret = 1;	for (this_char = strtok(options,","); this_char; this_char = strtok(NULL,",")) {		if ((value = strchr(this_char,'=')) != NULL) {			save = *value;			savep = value;			*value++ = 0;		}		if (!strcmp(this_char,"utf8")) {			ret = simple_getbool(value, &val);			if (ret) opts->utf8 = val;		} else if (!strcmp(this_char,"uni_xlate")) {			ret = simple_getbool(value, &val);			if (ret) opts->unicode_xlate = val;		} else if (!strcmp(this_char,"posix")) {			ret = simple_getbool(value, &val);			if (ret) opts->posixfs = val;		} else if (!strcmp(this_char,"nonumtail")) {			ret = simple_getbool(value, &val);			if (ret) {				opts->numtail = !val;			}		}		if (this_char != options)			*(this_char-1) = ',';		if (value) {			*savep = save;		}		if (ret == 0) {			return 0;		}	}	if (opts->unicode_xlate) {		opts->utf8 = 0;	}	return 1;}static inline unsigned charvfat_getlower(struct nls_table *t, unsigned char c){	return t->charset2lower[c];}static inline unsigned charvfat_tolower(struct nls_table *t, unsigned char c){	unsigned char nc = t->charset2lower[c];	return nc ? nc : c;}static inline unsigned charvfat_getupper(struct nls_table *t, unsigned char c){	return t->charset2upper[c];}static inline unsigned charvfat_toupper(struct nls_table *t, unsigned char c){	unsigned char nc = t->charset2upper[c];	return nc ? nc : c;}static intvfat_strnicmp(struct nls_table *t, const unsigned char *s1,					const unsigned char *s2, int len){	while(len--)		if (vfat_tolower(t, *s1++) != vfat_tolower(t, *s2++))			return 1;	return 0;}static inline intvfat_uni2short(struct nls_table *t, wchar_t uc, unsigned char *op, int bound){	int charlen;	if ( (charlen = t->uni2char(uc, op, bound)) < 0)		charlen = 0;	return charlen;}static inline intvfat_uni2upper_short(struct nls_table *t, wchar_t uc, char *op, int bound){	int chi, chl;	if ( (chl = t->uni2char(uc, op, bound)) < 0)		chl = 0;	for (chi = 0; chi < chl; chi++)		op[chi] = vfat_toupper(t, op[chi]);	return chl;}/* * Compute the hash for the vfat name corresponding to the dentry. * Note: if the name is invalid, we leave the hash code unchanged so * that the existing dentry can be used. The vfat fs routines will * return ENOENT or EINVAL as appropriate. */static int vfat_hash(struct dentry *dentry, struct qstr *qstr){	const char *name;	int len;	len = qstr->len;	name = qstr->name;	while (len && name[len-1] == '.')		len--;	qstr->hash = full_name_hash(name, len);	return 0;}/* * Compute the hash for the vfat name corresponding to the dentry. * Note: if the name is invalid, we leave the hash code unchanged so * that the existing dentry can be used. The vfat fs routines will * return ENOENT or EINVAL as appropriate. */static int vfat_hashi(struct dentry *dentry, struct qstr *qstr){	struct nls_table *t = MSDOS_SB(dentry->d_inode->i_sb)->nls_io;	const char *name;	int len;	unsigned long hash;	len = qstr->len;	name = qstr->name;	while (len && name[len-1] == '.')		len--;	hash = init_name_hash();	while (len--)		hash = partial_name_hash(vfat_tolower(t, *name++), hash);	qstr->hash = end_name_hash(hash);	return 0;}/* * Case insensitive compare of two vfat names. */static int vfat_cmpi(struct dentry *dentry, struct qstr *a, struct qstr *b){	struct nls_table *t = MSDOS_SB(dentry->d_inode->i_sb)->nls_io;	int alen, blen;	/* A filename cannot end in '.' or we treat it like it has none */	alen = a->len;	blen = b->len;	while (alen && a->name[alen-1] == '.')		alen--;	while (blen && b->name[blen-1] == '.')		blen--;	if (alen == blen) {		if (vfat_strnicmp(t, a->name, b->name, alen) == 0)			return 0;	}	return 1;}/* * Case sensitive compare of two vfat names. */static int vfat_cmp(struct dentry *dentry, struct qstr *a, struct qstr *b){	int alen, blen;	/* A filename cannot end in '.' or we treat it like it has none */	alen = a->len;	blen = b->len;	while (alen && a->name[alen-1] == '.')		alen--;	while (blen && b->name[blen-1] == '.')		blen--;	if (alen == blen) {		if (strncmp(a->name, b->name, alen) == 0)			return 0;	}	return 1;}#ifdef DEBUGstatic voidcheck_stack(const char *fname, int lineno){	int stack_level;	char *pg_dir;	stack_level = (long)(&pg_dir)-current->kernel_stack_page;	if (stack_level < 0)	        printk("*-*-*-* vfat kstack overflow in %s line %d: SL=%d\n",		       fname, lineno, stack_level);	else if (stack_level < 500)	        printk("*-*-*-* vfat kstack low in %s line %d: SL=%d\n",		       fname, lineno, stack_level);#if 0	else		printk("------- vfat kstack ok in %s line %d: SL=%d\n",		       fname, lineno, stack_level);#endif#if 0	if (*(unsigned long *) current->kernel_stack_page != STACK_MAGIC) {		printk("******* vfat stack corruption detected in %s at line %d\n",		       fname, lineno);	}#endif}static int debug = 0;static void dump_fat(struct super_block *sb,int start){	printk("[");	while (start) {		printk("%d ",start);		start = fat_access(sb,start,-1);		if (!start) {			printk("ERROR");			break;		}		if (start == -1) break;	}	printk("]\n");}static void dump_de(struct msdos_dir_entry *de){	int i;	unsigned char *p = (unsigned char *) de;	printk("[");	for (i = 0; i < 32; i++, p++) {		printk("%02x ", *p);	}	printk("]\n");}#endif/* MS-DOS "device special files" */static const char *reserved3_names[] = {	"con     ", "prn     ", "nul     ", "aux     ", NULL};static const char *reserved4_names[] = {	"com1    ", "com2    ", "com3    ", "com4    ", "com5    ",	"com6    ", "com7    ", "com8    ", "com9    ",	"lpt1    ", "lpt2    ", "lpt3    ", "lpt4    ", "lpt5    ",	"lpt6    ", "lpt7    ", "lpt8    ", "lpt9    ",	NULL };/* Characters that are undesirable in an MS-DOS file name */static char bad_chars[] = "*?<>|\":/\\";static char replace_chars[] = "[];,+=";/* Checks the validity of a long MS-DOS filename *//* Returns negative number on error, 0 for a normal * return, and 1 for . or .. */static int vfat_valid_longname(const char *name, int len, int xlate){	const char **reserved, *walk;	unsigned char c;	int i, baselen;	if (len && name[len-1] == ' ') return -EINVAL;	if (len >= 256) return -EINVAL;	for (i = 0; i < len; i++) {		c = name[i];		if (xlate && c == ':') continue;		if (strchr(bad_chars,c)) {			return -EINVAL;		}	} 	if (len < 3) return 0;	for (walk = name; *walk != 0 && *walk != '.'; walk++);	baselen = walk - name;	if (baselen == 3) {		for (reserved = reserved3_names; *reserved; reserved++) {			if (!strnicmp(name,*reserved,baselen))				return -EINVAL;		}	} else if (baselen == 4) {		for (reserved = reserved4_names; *reserved; reserved++) {			if (!strnicmp(name,*reserved,baselen))				return -EINVAL;		}	}	return 0;}static int vfat_valid_shortname(struct nls_table *nls, wchar_t *name, int len){	wchar_t *walk;	unsigned char c, charbuf[NLS_MAX_CHARSET_SIZE];	int chl, chi;	int space;	if (vfat_uni2upper_short(nls, *name, charbuf, NLS_MAX_CHARSET_SIZE) == 0)		return -EINVAL;	if (IS_FREE(charbuf))		return -EINVAL;	chl = 0;	c = 0;	space = 1; /* disallow names starting with a dot */	for (walk = name; len && walk-name < 8;) {		len--;		chl = nls->uni2char(*walk++, charbuf, NLS_MAX_CHARSET_SIZE);

⌨️ 快捷键说明

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