namei.c 2.7 KB

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