Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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> |
| 11 | #include <linux/efs_fs.h> |
| 12 | #include <linux/smp_lock.h> |
Christoph Hellwig | 05da080 | 2007-10-21 16:42:09 -0700 | [diff] [blame] | 13 | #include <linux/exportfs.h> |
| 14 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | |
| 16 | static 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 | |
| 62 | struct 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 Howells | 298384cd | 2008-02-07 00:15:34 -0800 | [diff] [blame] | 69 | inode = efs_iget(dir->i_sb, inodenum); |
| 70 | if (IS_ERR(inode)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | unlock_kernel(); |
David Howells | 298384cd | 2008-02-07 00:15:34 -0800 | [diff] [blame] | 72 | return ERR_CAST(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | unlock_kernel(); |
| 76 | |
| 77 | d_add(dentry, inode); |
| 78 | return NULL; |
| 79 | } |
| 80 | |
Christoph Hellwig | 05da080 | 2007-10-21 16:42:09 -0700 | [diff] [blame] | 81 | static struct inode *efs_nfs_get_inode(struct super_block *sb, u64 ino, |
| 82 | u32 generation) |
Christoph Hellwig | 5ca2960 | 2007-07-17 04:04:29 -0700 | [diff] [blame] | 83 | { |
Christoph Hellwig | 5ca2960 | 2007-07-17 04:04:29 -0700 | [diff] [blame] | 84 | struct inode *inode; |
Christoph Hellwig | 5ca2960 | 2007-07-17 04:04:29 -0700 | [diff] [blame] | 85 | |
| 86 | if (ino == 0) |
| 87 | return ERR_PTR(-ESTALE); |
David Howells | 298384cd | 2008-02-07 00:15:34 -0800 | [diff] [blame] | 88 | inode = efs_iget(sb, ino); |
| 89 | if (IS_ERR(inode)) |
| 90 | return ERR_CAST(inode); |
Christoph Hellwig | 5ca2960 | 2007-07-17 04:04:29 -0700 | [diff] [blame] | 91 | |
David Howells | 298384cd | 2008-02-07 00:15:34 -0800 | [diff] [blame] | 92 | if (generation && inode->i_generation != generation) { |
Christoph Hellwig | 05da080 | 2007-10-21 16:42:09 -0700 | [diff] [blame] | 93 | iput(inode); |
| 94 | return ERR_PTR(-ESTALE); |
Christoph Hellwig | 5ca2960 | 2007-07-17 04:04:29 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Christoph Hellwig | 05da080 | 2007-10-21 16:42:09 -0700 | [diff] [blame] | 97 | return inode; |
| 98 | } |
Christoph Hellwig | 5ca2960 | 2007-07-17 04:04:29 -0700 | [diff] [blame] | 99 | |
Christoph Hellwig | 05da080 | 2007-10-21 16:42:09 -0700 | [diff] [blame] | 100 | struct dentry *efs_fh_to_dentry(struct super_block *sb, struct fid *fid, |
| 101 | int fh_len, int fh_type) |
| 102 | { |
| 103 | return generic_fh_to_dentry(sb, fid, fh_len, fh_type, |
| 104 | efs_nfs_get_inode); |
| 105 | } |
| 106 | |
| 107 | struct dentry *efs_fh_to_parent(struct super_block *sb, struct fid *fid, |
| 108 | int fh_len, int fh_type) |
| 109 | { |
| 110 | return generic_fh_to_parent(sb, fid, fh_len, fh_type, |
| 111 | efs_nfs_get_inode); |
Christoph Hellwig | 5ca2960 | 2007-07-17 04:04:29 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | struct dentry *efs_get_parent(struct dentry *child) |
| 115 | { |
| 116 | struct dentry *parent; |
| 117 | struct inode *inode; |
| 118 | efs_ino_t ino; |
David Howells | 298384cd | 2008-02-07 00:15:34 -0800 | [diff] [blame] | 119 | long error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | |
| 121 | lock_kernel(); |
| 122 | |
| 123 | error = -ENOENT; |
| 124 | ino = efs_find_entry(child->d_inode, "..", 2); |
| 125 | if (!ino) |
| 126 | goto fail; |
| 127 | |
David Howells | 298384cd | 2008-02-07 00:15:34 -0800 | [diff] [blame] | 128 | inode = efs_iget(child->d_inode->i_sb, ino); |
| 129 | if (IS_ERR(inode)) { |
| 130 | error = PTR_ERR(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | goto fail; |
David Howells | 298384cd | 2008-02-07 00:15:34 -0800 | [diff] [blame] | 132 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | |
| 134 | error = -ENOMEM; |
| 135 | parent = d_alloc_anon(inode); |
| 136 | if (!parent) |
| 137 | goto fail_iput; |
| 138 | |
| 139 | unlock_kernel(); |
| 140 | return parent; |
| 141 | |
| 142 | fail_iput: |
| 143 | iput(inode); |
| 144 | fail: |
| 145 | unlock_kernel(); |
| 146 | return ERR_PTR(error); |
| 147 | } |