Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 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 Hellwig | 05da080 | 2007-10-21 16:42:09 -0700 | [diff] [blame] | 12 | #include <linux/exportfs.h> |
Christoph Hellwig | 45254b4 | 2008-02-23 15:23:51 -0800 | [diff] [blame] | 13 | #include "efs.h" |
Christoph Hellwig | 05da080 | 2007-10-21 16:42:09 -0700 | [diff] [blame] | 14 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | |
Fabian Frederick | ca35664 | 2014-08-08 14:19:39 -0700 | [diff] [blame] | 16 | static efs_ino_t efs_find_entry(struct inode *inode, const char *name, int len) |
| 17 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | 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 Frederick | f403d1d | 2014-06-04 16:12:12 -0700 | [diff] [blame] | 28 | pr_warn("%s(): directory size not a multiple of EFS_DIRBSIZE\n", |
| 29 | __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
| 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 Frederick | f403d1d | 2014-06-04 16:12:12 -0700 | [diff] [blame] | 35 | pr_err("%s(): failed to read dir block %d\n", |
| 36 | __func__, block); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | return 0; |
| 38 | } |
| 39 | |
| 40 | dirblock = (struct efs_dir *) bh->b_data; |
| 41 | |
| 42 | if (be16_to_cpu(dirblock->magic) != EFS_DIRBLK_MAGIC) { |
Fabian Frederick | f403d1d | 2014-06-04 16:12:12 -0700 | [diff] [blame] | 43 | pr_err("%s(): invalid directory block\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | brelse(bh); |
Fabian Frederick | ca35664 | 2014-08-08 14:19:39 -0700 | [diff] [blame] | 45 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Fabian Frederick | ca35664 | 2014-08-08 14:19:39 -0700 | [diff] [blame] | 48 | for (slot = 0; slot < dirblock->slots; slot++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | 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 Frederick | ca35664 | 2014-08-08 14:19:39 -0700 | [diff] [blame] | 57 | return inodenum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | } |
| 59 | } |
| 60 | brelse(bh); |
| 61 | } |
Fabian Frederick | ca35664 | 2014-08-08 14:19:39 -0700 | [diff] [blame] | 62 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 65 | struct dentry *efs_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags) |
| 66 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | efs_ino_t inodenum; |
Al Viro | a904937 | 2011-07-08 21:20:11 -0400 | [diff] [blame] | 68 | struct inode *inode = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | inodenum = efs_find_entry(dir, dentry->d_name.name, dentry->d_name.len); |
Al Viro | a904937 | 2011-07-08 21:20:11 -0400 | [diff] [blame] | 71 | if (inodenum) |
David Howells | 298384cd | 2008-02-07 00:15:34 -0800 | [diff] [blame] | 72 | inode = efs_iget(dir->i_sb, inodenum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | |
Al Viro | 2d8a10c | 2008-08-11 11:33:57 -0400 | [diff] [blame] | 74 | return d_splice_alias(inode, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Christoph Hellwig | 05da080 | 2007-10-21 16:42:09 -0700 | [diff] [blame] | 77 | static struct inode *efs_nfs_get_inode(struct super_block *sb, u64 ino, |
| 78 | u32 generation) |
Christoph Hellwig | 5ca2960 | 2007-07-17 04:04:29 -0700 | [diff] [blame] | 79 | { |
Christoph Hellwig | 5ca2960 | 2007-07-17 04:04:29 -0700 | [diff] [blame] | 80 | struct inode *inode; |
Christoph Hellwig | 5ca2960 | 2007-07-17 04:04:29 -0700 | [diff] [blame] | 81 | |
| 82 | if (ino == 0) |
| 83 | return ERR_PTR(-ESTALE); |
David Howells | 298384cd | 2008-02-07 00:15:34 -0800 | [diff] [blame] | 84 | inode = efs_iget(sb, ino); |
| 85 | if (IS_ERR(inode)) |
| 86 | return ERR_CAST(inode); |
Christoph Hellwig | 5ca2960 | 2007-07-17 04:04:29 -0700 | [diff] [blame] | 87 | |
David Howells | 298384cd | 2008-02-07 00:15:34 -0800 | [diff] [blame] | 88 | if (generation && inode->i_generation != generation) { |
Christoph Hellwig | 05da080 | 2007-10-21 16:42:09 -0700 | [diff] [blame] | 89 | iput(inode); |
| 90 | return ERR_PTR(-ESTALE); |
Christoph Hellwig | 5ca2960 | 2007-07-17 04:04:29 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Christoph Hellwig | 05da080 | 2007-10-21 16:42:09 -0700 | [diff] [blame] | 93 | return inode; |
| 94 | } |
Christoph Hellwig | 5ca2960 | 2007-07-17 04:04:29 -0700 | [diff] [blame] | 95 | |
Christoph Hellwig | 05da080 | 2007-10-21 16:42:09 -0700 | [diff] [blame] | 96 | struct 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 | |
| 103 | struct 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 Hellwig | 5ca2960 | 2007-07-17 04:04:29 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | struct dentry *efs_get_parent(struct dentry *child) |
| 111 | { |
Christoph Hellwig | 4400372 | 2008-08-11 15:49:04 +0200 | [diff] [blame] | 112 | struct dentry *parent = ERR_PTR(-ENOENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | efs_ino_t ino; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 115 | ino = efs_find_entry(d_inode(child), "..", 2); |
Christoph Hellwig | 4400372 | 2008-08-11 15:49:04 +0200 | [diff] [blame] | 116 | if (ino) |
Al Viro | fc64005 | 2016-04-10 01:33:30 -0400 | [diff] [blame] | 117 | parent = d_obtain_alias(efs_iget(child->d_sb, ino)); |
Christoph Hellwig | 4400372 | 2008-08-11 15:49:04 +0200 | [diff] [blame] | 118 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | return parent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | } |