1 /*
2  * namei.c
3  *
4  * Copyright (c) 1999 Al Smith
5  *
6  * Portions derived from work (c) 1995,1996 Christian Vogelgsang.
7  */
8 
9 #include <linux/string.h>
10 #include <linux/efs_fs.h>
11 
efs_find_entry(struct inode * inode,const char * name,int len)12 static efs_ino_t efs_find_entry(struct inode *inode, const char *name, int len) {
13 	struct buffer_head *bh;
14 
15 	int			slot, namelen;
16 	char			*nameptr;
17 	struct efs_dir		*dirblock;
18 	struct efs_dentry	*dirslot;
19 	efs_ino_t		inodenum;
20 	efs_block_t		block;
21 
22 	if (inode->i_size & (EFS_DIRBSIZE-1))
23 		printk(KERN_WARNING "EFS: WARNING: find_entry(): directory size not a multiple of EFS_DIRBSIZE\n");
24 
25 	for(block = 0; block < inode->i_blocks; block++) {
26 
27 		bh = sb_bread(inode->i_sb, efs_bmap(inode, block));
28 		if (!bh) {
29 			printk(KERN_ERR "EFS: find_entry(): failed to read dir block %d\n", block);
30 			return 0;
31 		}
32 
33 		dirblock = (struct efs_dir *) bh->b_data;
34 
35 		if (be16_to_cpu(dirblock->magic) != EFS_DIRBLK_MAGIC) {
36 			printk(KERN_ERR "EFS: find_entry(): invalid directory block\n");
37 			brelse(bh);
38 			return(0);
39 		}
40 
41 		for(slot = 0; slot < dirblock->slots; slot++) {
42 			dirslot  = (struct efs_dentry *) (((char *) bh->b_data) + EFS_SLOTAT(dirblock, slot));
43 
44 			namelen  = dirslot->namelen;
45 			nameptr  = dirslot->name;
46 
47 			if ((namelen == len) && (!memcmp(name, nameptr, len))) {
48 				inodenum = be32_to_cpu(dirslot->inode);
49 				brelse(bh);
50 				return(inodenum);
51 			}
52 		}
53 		brelse(bh);
54 	}
55 	return(0);
56 }
57 
efs_lookup(struct inode * dir,struct dentry * dentry)58 struct dentry *efs_lookup(struct inode *dir, struct dentry *dentry) {
59 	efs_ino_t inodenum;
60 	struct inode * inode;
61 
62 	if (!dir || !S_ISDIR(dir->i_mode))
63 		return ERR_PTR(-ENOENT);
64 
65 	inode = NULL;
66 
67 	inodenum = efs_find_entry(dir, dentry->d_name.name, dentry->d_name.len);
68 	if (inodenum) {
69 		if (!(inode = iget(dir->i_sb, inodenum)))
70 			return ERR_PTR(-EACCES);
71 	}
72 
73 	d_add(dentry, inode);
74 	return NULL;
75 }
76 
77