📄 namei.c
字号:
/* * Linux/fs/xiafs/namei.c * * Copyright (C) Q. Frank Xia, 1993. * * Based on Linus' minix/namei.c * Copyright (C) Linus Torvalds, 1991, 1992. * * This software may be redistributed per Linux Copyright. */#include <linux/sched.h>#include <linux/xia_fs.h>#include <linux/kernel.h>#include <linux/string.h>#include <linux/stat.h>#include <linux/fcntl.h>#include <linux/errno.h>#include <asm/segment.h>#include "xiafs_mac.h"#define RNDUP4(x) ((3+(u_long)(x)) & ~3)/* * ok, we cannot use strncmp, as the name is not in our data space. * Thus we'll have to use xiafs_match. No big problem. Match also makes * some sanity tests. * * NOTE! unlike strncmp, xiafs_match returns 1 for success, 0 for failure. */static int xiafs_match(int len, const char * name, struct xiafs_direct * dep){ int i; if (!dep || !dep->d_ino || len > _XIAFS_NAME_LEN) return 0; /* "" means "." ---> so paths like "/usr/lib//libc.a" work */ if (!len && (dep->d_name[0]=='.') && (dep->d_name[1]=='\0')) return 1; if (len != dep->d_name_len) return 0; for (i=0; i < len; i++) if (*name++ != dep->d_name[i]) return 0; return 1;}/* * xiafs_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. */static struct buffer_head * xiafs_find_entry(struct inode * inode, const char * name, int namelen, struct xiafs_direct ** res_dir, struct xiafs_direct ** res_pre){ int i, zones, pos; struct buffer_head * bh; struct xiafs_direct * dep, * dep_pre; *res_dir = NULL; if (!inode) return NULL; if (namelen > _XIAFS_NAME_LEN) return NULL; if (inode->i_size & (XIAFS_ZSIZE(inode->i_sb) - 1)) { printk("XIA-FS: bad dir size (%s %d)\n", WHERE_ERR); return NULL; } zones=inode->i_size >> XIAFS_ZSIZE_BITS(inode->i_sb); for (i=0; i < zones; i++ ) { bh = xiafs_bread(inode, i, 0); if (!bh) continue; dep_pre=dep=(struct xiafs_direct *)bh->b_data; if (!i && (dep->d_rec_len != 12 || !dep->d_ino || dep->d_name_len != 1 || strcmp(dep->d_name, "."))) { printk("XIA-FS: bad directory (%s %d)\n", WHERE_ERR); brelse(bh); return NULL; } pos = 0; while ( pos < XIAFS_ZSIZE(inode->i_sb) ) { if (dep->d_ino > inode->i_sb->u.xiafs_sb.s_ninodes || dep->d_rec_len < 12 || dep->d_rec_len+(char *)dep > bh->b_data+XIAFS_ZSIZE(inode->i_sb) || dep->d_name_len + 8 > dep->d_rec_len || dep->d_name_len <= 0 || dep->d_name[dep->d_name_len] ) { brelse(bh); return NULL; } if (xiafs_match(namelen, name, dep)) { *res_dir=dep; if (res_pre) *res_pre=dep_pre; return bh; } pos += dep->d_rec_len; dep_pre=dep; dep=(struct xiafs_direct *)(bh->b_data + pos); } brelse(bh); if (pos > XIAFS_ZSIZE(inode->i_sb)) { printk("XIA-FS: bad directory (%s %d)\n", WHERE_ERR); return NULL; } } return NULL;}int xiafs_lookup(struct inode * dir, const char * name, int len, struct inode ** result){ int ino; struct xiafs_direct * dep; struct buffer_head * bh; *result = NULL; if (!dir) return -ENOENT; if (!S_ISDIR(dir->i_mode)) { iput(dir); return -ENOENT; } if (!(bh = xiafs_find_entry(dir, name, len, &dep, NULL))) { iput(dir); return -ENOENT; } ino = dep->d_ino; brelse(bh); if (!(*result = iget(dir->i_sb, ino))) { iput(dir); return -EACCES; } iput(dir); return 0;}/* * xiafs_add_entry() * * adds a file entry to the specified directory, using the same * semantics as xiafs_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 * xiafs_add_entry(struct inode * dir, const char * name, int namelen, struct xiafs_direct ** res_dir, struct xiafs_direct ** res_pre){ int i, pos, offset; struct buffer_head * bh; struct xiafs_direct * de, * de_pre; *res_dir = NULL; if (!dir || !namelen || namelen > _XIAFS_NAME_LEN) return NULL; if (dir->i_size & (XIAFS_ZSIZE(dir->i_sb) - 1)) { printk("XIA-FS: bad dir size (%s %d)\n", WHERE_ERR); return NULL; } pos=0; for ( ; ; ) { bh = xiafs_bread(dir, pos >> XIAFS_ZSIZE_BITS(dir->i_sb), pos ? 1:0); if (!bh) return NULL; de_pre=de=(struct xiafs_direct *)bh->b_data; if (!pos) { if (de->d_rec_len != 12 || !de->d_ino || de->d_name_len != 1 || strcmp(de->d_name, ".")) { printk("XIA-FS: bad directory (%s %d)\n", WHERE_ERR); brelse(bh); return NULL; } offset = 12; de_pre=de=(struct xiafs_direct *)(bh->b_data+12); } else offset = 0; while (offset < XIAFS_ZSIZE(dir->i_sb)) { if (pos >= dir->i_size) { de->d_ino=0; de->d_name_len=0; de->d_name[0]=0; de->d_rec_len=XIAFS_ZSIZE(dir->i_sb); dir->i_size += XIAFS_ZSIZE(dir->i_sb); dir->i_dirt = 1; } else { if (de->d_ino > dir->i_sb->u.xiafs_sb.s_ninodes || de->d_rec_len < 12 || (char *)de+de->d_rec_len > bh->b_data+XIAFS_ZSIZE(dir->i_sb) || de->d_name_len + 8 > de->d_rec_len || de->d_name[de->d_name_len]) { printk("XIA-FS: bad directory entry (%s %d)\n", WHERE_ERR); brelse(bh); return NULL; } if (de->d_ino && RNDUP4(de->d_name_len)+RNDUP4(namelen)+16<=de->d_rec_len) { i=RNDUP4(de->d_name_len)+8; de_pre=de; de=(struct xiafs_direct *)(i+(u_char *)de_pre); de->d_ino=0; de->d_rec_len=de_pre->d_rec_len-i; de_pre->d_rec_len=i; } } if (!de->d_ino && RNDUP4(namelen)+8 <= de->d_rec_len) { /* * XXX all times should be set by caller upon successful * completion. */ dir->i_ctime = dir->i_mtime = CURRENT_TIME; dir->i_dirt = 1; memcpy(de->d_name, name, namelen); de->d_name[namelen]=0; de->d_name_len=namelen; bh->b_dirt = 1; *res_dir = de; if (res_pre) *res_pre = de_pre; return bh; } offset+=de->d_rec_len; de_pre=de; de=(struct xiafs_direct *)(bh->b_data+offset); } brelse(bh); if (offset > XIAFS_ZSIZE(dir->i_sb)) { printk("XIA-FS: bad directory (%s %d)\n", WHERE_ERR); return NULL; } pos+=XIAFS_ZSIZE(dir->i_sb); } return NULL;}int xiafs_create(struct inode * dir, const char * name, int len, int mode, struct inode ** result){ struct inode * inode; struct buffer_head * bh; struct xiafs_direct * de; *result = NULL; if (!dir) return -ENOENT; inode = xiafs_new_inode(dir); if (!inode) { iput(dir); return -ENOSPC; } inode->i_op = &xiafs_file_inode_operations; inode->i_mode = mode; inode->i_dirt = 1; bh = xiafs_add_entry(dir, name, len, &de, NULL); if (!bh) { inode->i_nlink--; inode->i_dirt = 1; iput(inode); iput(dir); return -ENOSPC; } de->d_ino = inode->i_ino; bh->b_dirt = 1; brelse(bh); iput(dir); *result = inode; return 0;}int xiafs_mknod(struct inode *dir, const char *name, int len, int mode, int rdev){ struct inode * inode; struct buffer_head * bh; struct xiafs_direct * de; if (!dir) return -ENOENT; bh = xiafs_find_entry(dir,name,len,&de, NULL); if (bh) { brelse(bh); iput(dir); return -EEXIST; } inode = xiafs_new_inode(dir); if (!inode) { iput(dir); return -ENOSPC; } inode->i_uid = current->euid; inode->i_mode = mode; inode->i_op = NULL; if (S_ISREG(inode->i_mode)) inode->i_op = &xiafs_file_inode_operations; else if (S_ISDIR(inode->i_mode)) { inode->i_op = &xiafs_dir_inode_operations; if (dir->i_mode & S_ISGID) inode->i_mode |= S_ISGID; } else if (S_ISLNK(inode->i_mode)) inode->i_op = &xiafs_symlink_inode_operations; else if (S_ISCHR(inode->i_mode)) inode->i_op = &chrdev_inode_operations; else if (S_ISBLK(inode->i_mode)) inode->i_op = &blkdev_inode_operations; else if (S_ISFIFO(inode->i_mode)) init_fifo(inode); if (S_ISBLK(mode) || S_ISCHR(mode)) inode->i_rdev = rdev; inode->i_atime = inode->i_ctime = inode->i_atime = CURRENT_TIME; inode->i_dirt = 1; bh = xiafs_add_entry(dir, name, len, &de, NULL); if (!bh) { inode->i_nlink--; inode->i_dirt = 1; iput(inode); iput(dir); return -ENOSPC; } de->d_ino = inode->i_ino; bh->b_dirt = 1; brelse(bh); iput(dir); iput(inode); return 0;}int xiafs_mkdir(struct inode * dir, const char * name, int len, int mode){ struct inode * inode; struct buffer_head * bh, *dir_block; struct xiafs_direct * de; bh = xiafs_find_entry(dir,name,len,&de, NULL); if (bh) { brelse(bh); iput(dir); return -EEXIST; } if (dir->i_nlink > 64000) { iput(dir); return -EMLINK; } inode = xiafs_new_inode(dir); if (!inode) { iput(dir); return -ENOSPC; } inode->i_op = &xiafs_dir_inode_operations; inode->i_size = XIAFS_ZSIZE(dir->i_sb); inode->i_atime = inode->i_ctime = inode->i_mtime = CURRENT_TIME; dir_block = xiafs_bread(inode,0,1); if (!dir_block) { iput(dir); inode->i_nlink--; inode->i_dirt = 1; iput(inode); return -ENOSPC; } de = (struct xiafs_direct *) dir_block->b_data; de->d_ino=inode->i_ino; strcpy(de->d_name,"."); de->d_name_len=1; de->d_rec_len=12; de =(struct xiafs_direct *)(12 + dir_block->b_data); de->d_ino = dir->i_ino; strcpy(de->d_name,".."); de->d_name_len=2; de->d_rec_len=XIAFS_ZSIZE(dir->i_sb)-12; inode->i_nlink = 2; dir_block->b_dirt = 1; brelse(dir_block); inode->i_mode = S_IFDIR | (mode & S_IRWXUGO & ~current->umask); if (dir->i_mode & S_ISGID) inode->i_mode |= S_ISGID; inode->i_dirt = 1; bh = xiafs_add_entry(dir, name, len, &de, NULL); if (!bh) { iput(dir); inode->i_nlink=0; iput(inode); return -ENOSPC; } de->d_ino = inode->i_ino; bh->b_dirt = 1; dir->i_nlink++; dir->i_dirt = 1; iput(dir); iput(inode); brelse(bh); return 0;}/* * routine to check that the specified directory is empty (for rmdir) */static int empty_dir(struct inode * inode){ int i, zones, offset; struct buffer_head * bh; struct xiafs_direct * de; if (inode->i_size & (XIAFS_ZSIZE(inode->i_sb)-1) ) { printk("XIA-FS: bad directory (%s %d)\n", WHERE_ERR); return 1; } zones=inode->i_size >> XIAFS_ZSIZE_BITS(inode->i_sb); for (i=0; i < zones; i++) { bh = xiafs_bread(inode, i, 0); if (!i) { if (!bh) { printk("XIA-FS: bad directory (%s %d)\n", WHERE_ERR); return 1; } de=(struct xiafs_direct *)bh->b_data; if (de->d_ino != inode->i_ino || strcmp(".", de->d_name) ||
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -