blob: 356c044e2cd302276f43c19031e74b8f678de35b [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>
Christoph Hellwig05da0802007-10-21 16:42:09 -070011#include <linux/exportfs.h>
Christoph Hellwig45254b42008-02-23 15:23:51 -080012#include "efs.h"
Christoph Hellwig05da0802007-10-21 16:42:09 -070013
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
15static efs_ino_t efs_find_entry(struct inode *inode, const char *name, int len) {
16 struct buffer_head *bh;
17
18 int slot, namelen;
19 char *nameptr;
20 struct efs_dir *dirblock;
21 struct efs_dentry *dirslot;
22 efs_ino_t inodenum;
23 efs_block_t block;
24
25 if (inode->i_size & (EFS_DIRBSIZE-1))
Fabian Frederickf403d1d2014-06-04 16:12:12 -070026 pr_warn("%s(): directory size not a multiple of EFS_DIRBSIZE\n",
27 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29 for(block = 0; block < inode->i_blocks; block++) {
30
31 bh = sb_bread(inode->i_sb, efs_bmap(inode, block));
32 if (!bh) {
Fabian Frederickf403d1d2014-06-04 16:12:12 -070033 pr_err("%s(): failed to read dir block %d\n",
34 __func__, block);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 return 0;
36 }
37
38 dirblock = (struct efs_dir *) bh->b_data;
39
40 if (be16_to_cpu(dirblock->magic) != EFS_DIRBLK_MAGIC) {
Fabian Frederickf403d1d2014-06-04 16:12:12 -070041 pr_err("%s(): invalid directory block\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 brelse(bh);
43 return(0);
44 }
45
46 for(slot = 0; slot < dirblock->slots; slot++) {
47 dirslot = (struct efs_dentry *) (((char *) bh->b_data) + EFS_SLOTAT(dirblock, slot));
48
49 namelen = dirslot->namelen;
50 nameptr = dirslot->name;
51
52 if ((namelen == len) && (!memcmp(name, nameptr, len))) {
53 inodenum = be32_to_cpu(dirslot->inode);
54 brelse(bh);
55 return(inodenum);
56 }
57 }
58 brelse(bh);
59 }
60 return(0);
61}
62
Al Viro00cd8dd2012-06-10 17:13:09 -040063struct dentry *efs_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
64{
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 efs_ino_t inodenum;
Al Viroa9049372011-07-08 21:20:11 -040066 struct inode *inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 inodenum = efs_find_entry(dir, dentry->d_name.name, dentry->d_name.len);
Al Viroa9049372011-07-08 21:20:11 -040069 if (inodenum)
David Howells298384cd2008-02-07 00:15:34 -080070 inode = efs_iget(dir->i_sb, inodenum);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Al Viro2d8a10c2008-08-11 11:33:57 -040072 return d_splice_alias(inode, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073}
74
Christoph Hellwig05da0802007-10-21 16:42:09 -070075static struct inode *efs_nfs_get_inode(struct super_block *sb, u64 ino,
76 u32 generation)
Christoph Hellwig5ca29602007-07-17 04:04:29 -070077{
Christoph Hellwig5ca29602007-07-17 04:04:29 -070078 struct inode *inode;
Christoph Hellwig5ca29602007-07-17 04:04:29 -070079
80 if (ino == 0)
81 return ERR_PTR(-ESTALE);
David Howells298384cd2008-02-07 00:15:34 -080082 inode = efs_iget(sb, ino);
83 if (IS_ERR(inode))
84 return ERR_CAST(inode);
Christoph Hellwig5ca29602007-07-17 04:04:29 -070085
David Howells298384cd2008-02-07 00:15:34 -080086 if (generation && inode->i_generation != generation) {
Christoph Hellwig05da0802007-10-21 16:42:09 -070087 iput(inode);
88 return ERR_PTR(-ESTALE);
Christoph Hellwig5ca29602007-07-17 04:04:29 -070089 }
90
Christoph Hellwig05da0802007-10-21 16:42:09 -070091 return inode;
92}
Christoph Hellwig5ca29602007-07-17 04:04:29 -070093
Christoph Hellwig05da0802007-10-21 16:42:09 -070094struct dentry *efs_fh_to_dentry(struct super_block *sb, struct fid *fid,
95 int fh_len, int fh_type)
96{
97 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
98 efs_nfs_get_inode);
99}
100
101struct dentry *efs_fh_to_parent(struct super_block *sb, struct fid *fid,
102 int fh_len, int fh_type)
103{
104 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
105 efs_nfs_get_inode);
Christoph Hellwig5ca29602007-07-17 04:04:29 -0700106}
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108struct dentry *efs_get_parent(struct dentry *child)
109{
Christoph Hellwig44003722008-08-11 15:49:04 +0200110 struct dentry *parent = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 efs_ino_t ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 ino = efs_find_entry(child->d_inode, "..", 2);
Christoph Hellwig44003722008-08-11 15:49:04 +0200114 if (ino)
115 parent = d_obtain_alias(efs_iget(child->d_inode->i_sb, ino));
Christoph Hellwig44003722008-08-11 15:49:04 +0200116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 return parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118}