blob: c3fb5f9c4a441d44fab690b43c8b9a0549fe844e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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/buffer_head.h>
10#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/smp_lock.h>
Christoph Hellwig05da0802007-10-21 16:42:09 -070012#include <linux/exportfs.h>
Christoph Hellwig45254b42008-02-23 15:23:51 -080013#include "efs.h"
Christoph Hellwig05da0802007-10-21 16:42:09 -070014
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
16static efs_ino_t efs_find_entry(struct inode *inode, const char *name, int len) {
17 struct buffer_head *bh;
18
19 int slot, namelen;
20 char *nameptr;
21 struct efs_dir *dirblock;
22 struct efs_dentry *dirslot;
23 efs_ino_t inodenum;
24 efs_block_t block;
25
26 if (inode->i_size & (EFS_DIRBSIZE-1))
27 printk(KERN_WARNING "EFS: WARNING: find_entry(): directory size not a multiple of EFS_DIRBSIZE\n");
28
29 for(block = 0; block < inode->i_blocks; block++) {
30
31 bh = sb_bread(inode->i_sb, efs_bmap(inode, block));
32 if (!bh) {
33 printk(KERN_ERR "EFS: find_entry(): failed to read dir block %d\n", block);
34 return 0;
35 }
36
37 dirblock = (struct efs_dir *) bh->b_data;
38
39 if (be16_to_cpu(dirblock->magic) != EFS_DIRBLK_MAGIC) {
40 printk(KERN_ERR "EFS: find_entry(): invalid directory block\n");
41 brelse(bh);
42 return(0);
43 }
44
45 for(slot = 0; slot < dirblock->slots; slot++) {
46 dirslot = (struct efs_dentry *) (((char *) bh->b_data) + EFS_SLOTAT(dirblock, slot));
47
48 namelen = dirslot->namelen;
49 nameptr = dirslot->name;
50
51 if ((namelen == len) && (!memcmp(name, nameptr, len))) {
52 inodenum = be32_to_cpu(dirslot->inode);
53 brelse(bh);
54 return(inodenum);
55 }
56 }
57 brelse(bh);
58 }
59 return(0);
60}
61
62struct dentry *efs_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd) {
63 efs_ino_t inodenum;
64 struct inode * inode = NULL;
65
66 lock_kernel();
67 inodenum = efs_find_entry(dir, dentry->d_name.name, dentry->d_name.len);
68 if (inodenum) {
David Howells298384cd2008-02-07 00:15:34 -080069 inode = efs_iget(dir->i_sb, inodenum);
70 if (IS_ERR(inode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 unlock_kernel();
David Howells298384cd2008-02-07 00:15:34 -080072 return ERR_CAST(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 }
74 }
75 unlock_kernel();
76
Al Viro2d8a10c2008-08-11 11:33:57 -040077 return d_splice_alias(inode, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078}
79
Christoph Hellwig05da0802007-10-21 16:42:09 -070080static struct inode *efs_nfs_get_inode(struct super_block *sb, u64 ino,
81 u32 generation)
Christoph Hellwig5ca29602007-07-17 04:04:29 -070082{
Christoph Hellwig5ca29602007-07-17 04:04:29 -070083 struct inode *inode;
Christoph Hellwig5ca29602007-07-17 04:04:29 -070084
85 if (ino == 0)
86 return ERR_PTR(-ESTALE);
David Howells298384cd2008-02-07 00:15:34 -080087 inode = efs_iget(sb, ino);
88 if (IS_ERR(inode))
89 return ERR_CAST(inode);
Christoph Hellwig5ca29602007-07-17 04:04:29 -070090
David Howells298384cd2008-02-07 00:15:34 -080091 if (generation && inode->i_generation != generation) {
Christoph Hellwig05da0802007-10-21 16:42:09 -070092 iput(inode);
93 return ERR_PTR(-ESTALE);
Christoph Hellwig5ca29602007-07-17 04:04:29 -070094 }
95
Christoph Hellwig05da0802007-10-21 16:42:09 -070096 return inode;
97}
Christoph Hellwig5ca29602007-07-17 04:04:29 -070098
Christoph Hellwig05da0802007-10-21 16:42:09 -070099struct dentry *efs_fh_to_dentry(struct super_block *sb, struct fid *fid,
100 int fh_len, int fh_type)
101{
102 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
103 efs_nfs_get_inode);
104}
105
106struct dentry *efs_fh_to_parent(struct super_block *sb, struct fid *fid,
107 int fh_len, int fh_type)
108{
109 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
110 efs_nfs_get_inode);
Christoph Hellwig5ca29602007-07-17 04:04:29 -0700111}
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113struct dentry *efs_get_parent(struct dentry *child)
114{
Christoph Hellwig44003722008-08-11 15:49:04 +0200115 struct dentry *parent = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 efs_ino_t ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 ino = efs_find_entry(child->d_inode, "..", 2);
Christoph Hellwig44003722008-08-11 15:49:04 +0200120 if (ino)
121 parent = d_obtain_alias(efs_iget(child->d_inode->i_sb, ino));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 unlock_kernel();
Christoph Hellwig44003722008-08-11 15:49:04 +0200123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 return parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125}