blob: d34a40edcdb27cd4bd98a9273e70180c240970e4 [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
Fabian Frederickca356642014-08-08 14:19:39 -070015static efs_ino_t efs_find_entry(struct inode *inode, const char *name, int len)
16{
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 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))
Fabian Frederickf403d1d2014-06-04 16:12:12 -070027 pr_warn("%s(): directory size not a multiple of EFS_DIRBSIZE\n",
28 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30 for(block = 0; block < inode->i_blocks; block++) {
31
32 bh = sb_bread(inode->i_sb, efs_bmap(inode, block));
33 if (!bh) {
Fabian Frederickf403d1d2014-06-04 16:12:12 -070034 pr_err("%s(): failed to read dir block %d\n",
35 __func__, block);
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 return 0;
37 }
38
39 dirblock = (struct efs_dir *) bh->b_data;
40
41 if (be16_to_cpu(dirblock->magic) != EFS_DIRBLK_MAGIC) {
Fabian Frederickf403d1d2014-06-04 16:12:12 -070042 pr_err("%s(): invalid directory block\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 brelse(bh);
Fabian Frederickca356642014-08-08 14:19:39 -070044 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 }
46
Fabian Frederickca356642014-08-08 14:19:39 -070047 for (slot = 0; slot < dirblock->slots; slot++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 dirslot = (struct efs_dentry *) (((char *) bh->b_data) + EFS_SLOTAT(dirblock, slot));
49
50 namelen = dirslot->namelen;
51 nameptr = dirslot->name;
52
53 if ((namelen == len) && (!memcmp(name, nameptr, len))) {
54 inodenum = be32_to_cpu(dirslot->inode);
55 brelse(bh);
Fabian Frederickca356642014-08-08 14:19:39 -070056 return inodenum;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 }
58 }
59 brelse(bh);
60 }
Fabian Frederickca356642014-08-08 14:19:39 -070061 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062}
63
Al Viro00cd8dd2012-06-10 17:13:09 -040064struct dentry *efs_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
65{
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 efs_ino_t inodenum;
Al Viroa9049372011-07-08 21:20:11 -040067 struct inode *inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 inodenum = efs_find_entry(dir, dentry->d_name.name, dentry->d_name.len);
Al Viroa9049372011-07-08 21:20:11 -040070 if (inodenum)
David Howells298384cd2008-02-07 00:15:34 -080071 inode = efs_iget(dir->i_sb, inodenum);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Al Viro2d8a10c2008-08-11 11:33:57 -040073 return d_splice_alias(inode, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074}
75
Christoph Hellwig05da0802007-10-21 16:42:09 -070076static struct inode *efs_nfs_get_inode(struct super_block *sb, u64 ino,
77 u32 generation)
Christoph Hellwig5ca29602007-07-17 04:04:29 -070078{
Christoph Hellwig5ca29602007-07-17 04:04:29 -070079 struct inode *inode;
Christoph Hellwig5ca29602007-07-17 04:04:29 -070080
81 if (ino == 0)
82 return ERR_PTR(-ESTALE);
David Howells298384cd2008-02-07 00:15:34 -080083 inode = efs_iget(sb, ino);
84 if (IS_ERR(inode))
85 return ERR_CAST(inode);
Christoph Hellwig5ca29602007-07-17 04:04:29 -070086
David Howells298384cd2008-02-07 00:15:34 -080087 if (generation && inode->i_generation != generation) {
Christoph Hellwig05da0802007-10-21 16:42:09 -070088 iput(inode);
89 return ERR_PTR(-ESTALE);
Christoph Hellwig5ca29602007-07-17 04:04:29 -070090 }
91
Christoph Hellwig05da0802007-10-21 16:42:09 -070092 return inode;
93}
Christoph Hellwig5ca29602007-07-17 04:04:29 -070094
Christoph Hellwig05da0802007-10-21 16:42:09 -070095struct dentry *efs_fh_to_dentry(struct super_block *sb, struct fid *fid,
96 int fh_len, int fh_type)
97{
98 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
99 efs_nfs_get_inode);
100}
101
102struct dentry *efs_fh_to_parent(struct super_block *sb, struct fid *fid,
103 int fh_len, int fh_type)
104{
105 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
106 efs_nfs_get_inode);
Christoph Hellwig5ca29602007-07-17 04:04:29 -0700107}
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109struct dentry *efs_get_parent(struct dentry *child)
110{
Christoph Hellwig44003722008-08-11 15:49:04 +0200111 struct dentry *parent = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 efs_ino_t ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
David Howells2b0143b2015-03-17 22:25:59 +0000114 ino = efs_find_entry(d_inode(child), "..", 2);
Christoph Hellwig44003722008-08-11 15:49:04 +0200115 if (ino)
Al Virofc640052016-04-10 01:33:30 -0400116 parent = d_obtain_alias(efs_iget(child->d_sb, ino));
Christoph Hellwig44003722008-08-11 15:49:04 +0200117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 return parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119}