blob: 38961ee1d1af18e01e29fbdeeb114ad86d6361e7 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * namei.c
4 *
5 * Copyright (c) 1999 Al Smith
6 *
7 * Portions derived from work (c) 1995,1996 Christian Vogelgsang.
8 */
9
10#include <linux/buffer_head.h>
11#include <linux/string.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
Fabian Frederickca356642014-08-08 14:19:39 -070016static efs_ino_t efs_find_entry(struct inode *inode, const char *name, int len)
17{
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 struct buffer_head *bh;
19
20 int slot, namelen;
21 char *nameptr;
22 struct efs_dir *dirblock;
23 struct efs_dentry *dirslot;
24 efs_ino_t inodenum;
25 efs_block_t block;
26
27 if (inode->i_size & (EFS_DIRBSIZE-1))
Fabian Frederickf403d1d2014-06-04 16:12:12 -070028 pr_warn("%s(): directory size not a multiple of EFS_DIRBSIZE\n",
29 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31 for(block = 0; block < inode->i_blocks; block++) {
32
33 bh = sb_bread(inode->i_sb, efs_bmap(inode, block));
34 if (!bh) {
Fabian Frederickf403d1d2014-06-04 16:12:12 -070035 pr_err("%s(): failed to read dir block %d\n",
36 __func__, block);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 return 0;
38 }
39
40 dirblock = (struct efs_dir *) bh->b_data;
41
42 if (be16_to_cpu(dirblock->magic) != EFS_DIRBLK_MAGIC) {
Fabian Frederickf403d1d2014-06-04 16:12:12 -070043 pr_err("%s(): invalid directory block\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 brelse(bh);
Fabian Frederickca356642014-08-08 14:19:39 -070045 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 }
47
Fabian Frederickca356642014-08-08 14:19:39 -070048 for (slot = 0; slot < dirblock->slots; slot++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 dirslot = (struct efs_dentry *) (((char *) bh->b_data) + EFS_SLOTAT(dirblock, slot));
50
51 namelen = dirslot->namelen;
52 nameptr = dirslot->name;
53
54 if ((namelen == len) && (!memcmp(name, nameptr, len))) {
55 inodenum = be32_to_cpu(dirslot->inode);
56 brelse(bh);
Fabian Frederickca356642014-08-08 14:19:39 -070057 return inodenum;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 }
59 }
60 brelse(bh);
61 }
Fabian Frederickca356642014-08-08 14:19:39 -070062 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063}
64
Al Viro00cd8dd2012-06-10 17:13:09 -040065struct dentry *efs_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
66{
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 efs_ino_t inodenum;
Al Viroa9049372011-07-08 21:20:11 -040068 struct inode *inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 inodenum = efs_find_entry(dir, dentry->d_name.name, dentry->d_name.len);
Al Viroa9049372011-07-08 21:20:11 -040071 if (inodenum)
David Howells298384cd2008-02-07 00:15:34 -080072 inode = efs_iget(dir->i_sb, inodenum);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Al Viro2d8a10c2008-08-11 11:33:57 -040074 return d_splice_alias(inode, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075}
76
Christoph Hellwig05da0802007-10-21 16:42:09 -070077static struct inode *efs_nfs_get_inode(struct super_block *sb, u64 ino,
78 u32 generation)
Christoph Hellwig5ca29602007-07-17 04:04:29 -070079{
Christoph Hellwig5ca29602007-07-17 04:04:29 -070080 struct inode *inode;
Christoph Hellwig5ca29602007-07-17 04:04:29 -070081
82 if (ino == 0)
83 return ERR_PTR(-ESTALE);
David Howells298384cd2008-02-07 00:15:34 -080084 inode = efs_iget(sb, ino);
85 if (IS_ERR(inode))
86 return ERR_CAST(inode);
Christoph Hellwig5ca29602007-07-17 04:04:29 -070087
David Howells298384cd2008-02-07 00:15:34 -080088 if (generation && inode->i_generation != generation) {
Christoph Hellwig05da0802007-10-21 16:42:09 -070089 iput(inode);
90 return ERR_PTR(-ESTALE);
Christoph Hellwig5ca29602007-07-17 04:04:29 -070091 }
92
Christoph Hellwig05da0802007-10-21 16:42:09 -070093 return inode;
94}
Christoph Hellwig5ca29602007-07-17 04:04:29 -070095
Christoph Hellwig05da0802007-10-21 16:42:09 -070096struct dentry *efs_fh_to_dentry(struct super_block *sb, struct fid *fid,
97 int fh_len, int fh_type)
98{
99 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
100 efs_nfs_get_inode);
101}
102
103struct dentry *efs_fh_to_parent(struct super_block *sb, struct fid *fid,
104 int fh_len, int fh_type)
105{
106 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
107 efs_nfs_get_inode);
Christoph Hellwig5ca29602007-07-17 04:04:29 -0700108}
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110struct dentry *efs_get_parent(struct dentry *child)
111{
Christoph Hellwig44003722008-08-11 15:49:04 +0200112 struct dentry *parent = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 efs_ino_t ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
David Howells2b0143b2015-03-17 22:25:59 +0000115 ino = efs_find_entry(d_inode(child), "..", 2);
Christoph Hellwig44003722008-08-11 15:49:04 +0200116 if (ino)
Al Virofc640052016-04-10 01:33:30 -0400117 parent = d_obtain_alias(efs_iget(child->d_sb, ino));
Christoph Hellwig44003722008-08-11 15:49:04 +0200118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 return parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}