namei.c 2.9 KB

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