namei.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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/efs_fs.h>
  11. #include <linux/smp_lock.h>
  12. #include <linux/exportfs.h>
  13. static efs_ino_t efs_find_entry(struct inode *inode, const char *name, int len) {
  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. printk(KERN_WARNING "EFS: WARNING: find_entry(): directory size not a multiple of EFS_DIRBSIZE\n");
  23. for(block = 0; block < inode->i_blocks; block++) {
  24. bh = sb_bread(inode->i_sb, efs_bmap(inode, block));
  25. if (!bh) {
  26. printk(KERN_ERR "EFS: find_entry(): failed to read dir block %d\n", block);
  27. return 0;
  28. }
  29. dirblock = (struct efs_dir *) bh->b_data;
  30. if (be16_to_cpu(dirblock->magic) != EFS_DIRBLK_MAGIC) {
  31. printk(KERN_ERR "EFS: find_entry(): invalid directory block\n");
  32. brelse(bh);
  33. return(0);
  34. }
  35. for(slot = 0; slot < dirblock->slots; slot++) {
  36. dirslot = (struct efs_dentry *) (((char *) bh->b_data) + EFS_SLOTAT(dirblock, slot));
  37. namelen = dirslot->namelen;
  38. nameptr = dirslot->name;
  39. if ((namelen == len) && (!memcmp(name, nameptr, len))) {
  40. inodenum = be32_to_cpu(dirslot->inode);
  41. brelse(bh);
  42. return(inodenum);
  43. }
  44. }
  45. brelse(bh);
  46. }
  47. return(0);
  48. }
  49. struct dentry *efs_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd) {
  50. efs_ino_t inodenum;
  51. struct inode * inode = NULL;
  52. lock_kernel();
  53. inodenum = efs_find_entry(dir, dentry->d_name.name, dentry->d_name.len);
  54. if (inodenum) {
  55. if (!(inode = iget(dir->i_sb, inodenum))) {
  56. unlock_kernel();
  57. return ERR_PTR(-EACCES);
  58. }
  59. }
  60. unlock_kernel();
  61. d_add(dentry, inode);
  62. return NULL;
  63. }
  64. static struct inode *efs_nfs_get_inode(struct super_block *sb, u64 ino,
  65. u32 generation)
  66. {
  67. struct inode *inode;
  68. if (ino == 0)
  69. return ERR_PTR(-ESTALE);
  70. inode = iget(sb, ino);
  71. if (inode == NULL)
  72. return ERR_PTR(-ENOMEM);
  73. if (is_bad_inode(inode) ||
  74. (generation && inode->i_generation != generation)) {
  75. iput(inode);
  76. return ERR_PTR(-ESTALE);
  77. }
  78. return inode;
  79. }
  80. struct dentry *efs_fh_to_dentry(struct super_block *sb, struct fid *fid,
  81. int fh_len, int fh_type)
  82. {
  83. return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
  84. efs_nfs_get_inode);
  85. }
  86. struct dentry *efs_fh_to_parent(struct super_block *sb, struct fid *fid,
  87. int fh_len, int fh_type)
  88. {
  89. return generic_fh_to_parent(sb, fid, fh_len, fh_type,
  90. efs_nfs_get_inode);
  91. }
  92. struct dentry *efs_get_parent(struct dentry *child)
  93. {
  94. struct dentry *parent;
  95. struct inode *inode;
  96. efs_ino_t ino;
  97. int error;
  98. lock_kernel();
  99. error = -ENOENT;
  100. ino = efs_find_entry(child->d_inode, "..", 2);
  101. if (!ino)
  102. goto fail;
  103. error = -EACCES;
  104. inode = iget(child->d_inode->i_sb, ino);
  105. if (!inode)
  106. goto fail;
  107. error = -ENOMEM;
  108. parent = d_alloc_anon(inode);
  109. if (!parent)
  110. goto fail_iput;
  111. unlock_kernel();
  112. return parent;
  113. fail_iput:
  114. iput(inode);
  115. fail:
  116. unlock_kernel();
  117. return ERR_PTR(error);
  118. }