namei.c 2.6 KB

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