namei.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * namei.c
  3. *
  4. * Copyright (c) 1999 Al Smith
  5. *
  6. * Portions derived from work (c) 1995,1996 Christian Vogelgsang.
  7. */
  8. #include <linux/buffer_head.h>
  9. #include <linux/string.h>
  10. #include <linux/exportfs.h>
  11. #include "efs.h"
  12. static efs_ino_t efs_find_entry(struct inode *inode, const char *name, int len) {
  13. struct buffer_head *bh;
  14. int slot, namelen;
  15. char *nameptr;
  16. struct efs_dir *dirblock;
  17. struct efs_dentry *dirslot;
  18. efs_ino_t inodenum;
  19. efs_block_t block;
  20. if (inode->i_size & (EFS_DIRBSIZE-1))
  21. pr_warn("%s(): directory size not a multiple of EFS_DIRBSIZE\n",
  22. __func__);
  23. for(block = 0; block < inode->i_blocks; block++) {
  24. bh = sb_bread(inode->i_sb, efs_bmap(inode, block));
  25. if (!bh) {
  26. pr_err("%s(): failed to read dir block %d\n",
  27. __func__, block);
  28. return 0;
  29. }
  30. dirblock = (struct efs_dir *) bh->b_data;
  31. if (be16_to_cpu(dirblock->magic) != EFS_DIRBLK_MAGIC) {
  32. pr_err("%s(): invalid directory block\n", __func__);
  33. brelse(bh);
  34. return(0);
  35. }
  36. for(slot = 0; slot < dirblock->slots; slot++) {
  37. dirslot = (struct efs_dentry *) (((char *) bh->b_data) + EFS_SLOTAT(dirblock, slot));
  38. namelen = dirslot->namelen;
  39. nameptr = dirslot->name;
  40. if ((namelen == len) && (!memcmp(name, nameptr, len))) {
  41. inodenum = be32_to_cpu(dirslot->inode);
  42. brelse(bh);
  43. return(inodenum);
  44. }
  45. }
  46. brelse(bh);
  47. }
  48. return(0);
  49. }
  50. struct dentry *efs_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
  51. {
  52. efs_ino_t inodenum;
  53. struct inode *inode = NULL;
  54. inodenum = efs_find_entry(dir, dentry->d_name.name, dentry->d_name.len);
  55. if (inodenum)
  56. inode = efs_iget(dir->i_sb, inodenum);
  57. return d_splice_alias(inode, dentry);
  58. }
  59. static struct inode *efs_nfs_get_inode(struct super_block *sb, u64 ino,
  60. u32 generation)
  61. {
  62. struct inode *inode;
  63. if (ino == 0)
  64. return ERR_PTR(-ESTALE);
  65. inode = efs_iget(sb, ino);
  66. if (IS_ERR(inode))
  67. return ERR_CAST(inode);
  68. if (generation && inode->i_generation != generation) {
  69. iput(inode);
  70. return ERR_PTR(-ESTALE);
  71. }
  72. return inode;
  73. }
  74. struct dentry *efs_fh_to_dentry(struct super_block *sb, struct fid *fid,
  75. int fh_len, int fh_type)
  76. {
  77. return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
  78. efs_nfs_get_inode);
  79. }
  80. struct dentry *efs_fh_to_parent(struct super_block *sb, struct fid *fid,
  81. int fh_len, int fh_type)
  82. {
  83. return generic_fh_to_parent(sb, fid, fh_len, fh_type,
  84. efs_nfs_get_inode);
  85. }
  86. struct dentry *efs_get_parent(struct dentry *child)
  87. {
  88. struct dentry *parent = ERR_PTR(-ENOENT);
  89. efs_ino_t ino;
  90. ino = efs_find_entry(child->d_inode, "..", 2);
  91. if (ino)
  92. parent = d_obtain_alias(efs_iget(child->d_inode->i_sb, ino));
  93. return parent;
  94. }