namei.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * QNX4 file system, Linux implementation.
  3. *
  4. * Version : 0.2.1
  5. *
  6. * Using parts of the xiafs filesystem.
  7. *
  8. * History :
  9. *
  10. * 01-06-1998 by Richard Frowijn : first release.
  11. * 21-06-1998 by Frank Denis : dcache support, fixed error codes.
  12. * 04-07-1998 by Frank Denis : first step for rmdir/unlink.
  13. */
  14. #include <linux/buffer_head.h>
  15. #include "qnx4.h"
  16. /*
  17. * check if the filename is correct. For some obscure reason, qnx writes a
  18. * new file twice in the directory entry, first with all possible options at 0
  19. * and for a second time the way it is, they want us not to access the qnx
  20. * filesystem when whe are using linux.
  21. */
  22. static int qnx4_match(int len, const char *name,
  23. struct buffer_head *bh, unsigned long *offset)
  24. {
  25. struct qnx4_inode_entry *de;
  26. int namelen, thislen;
  27. if (bh == NULL) {
  28. printk(KERN_WARNING "qnx4: matching unassigned buffer !\n");
  29. return 0;
  30. }
  31. de = (struct qnx4_inode_entry *) (bh->b_data + *offset);
  32. *offset += QNX4_DIR_ENTRY_SIZE;
  33. if ((de->di_status & QNX4_FILE_LINK) != 0) {
  34. namelen = QNX4_NAME_MAX;
  35. } else {
  36. namelen = QNX4_SHORT_NAME_MAX;
  37. }
  38. /* "" means "." ---> so paths like "/usr/lib//libc.a" work */
  39. if (!len && (de->di_fname[0] == '.') && (de->di_fname[1] == '\0')) {
  40. return 1;
  41. }
  42. thislen = strlen( de->di_fname );
  43. if ( thislen > namelen )
  44. thislen = namelen;
  45. if (len != thislen) {
  46. return 0;
  47. }
  48. if (strncmp(name, de->di_fname, len) == 0) {
  49. if ((de->di_status & (QNX4_FILE_USED|QNX4_FILE_LINK)) != 0) {
  50. return 1;
  51. }
  52. }
  53. return 0;
  54. }
  55. static struct buffer_head *qnx4_find_entry(int len, struct inode *dir,
  56. const char *name, struct qnx4_inode_entry **res_dir, int *ino)
  57. {
  58. unsigned long block, offset, blkofs;
  59. struct buffer_head *bh;
  60. *res_dir = NULL;
  61. if (!dir->i_sb) {
  62. printk(KERN_WARNING "qnx4: no superblock on dir.\n");
  63. return NULL;
  64. }
  65. bh = NULL;
  66. block = offset = blkofs = 0;
  67. while (blkofs * QNX4_BLOCK_SIZE + offset < dir->i_size) {
  68. if (!bh) {
  69. bh = qnx4_bread(dir, blkofs, 0);
  70. if (!bh) {
  71. blkofs++;
  72. continue;
  73. }
  74. }
  75. *res_dir = (struct qnx4_inode_entry *) (bh->b_data + offset);
  76. if (qnx4_match(len, name, bh, &offset)) {
  77. block = qnx4_block_map( dir, blkofs );
  78. *ino = block * QNX4_INODES_PER_BLOCK +
  79. (offset / QNX4_DIR_ENTRY_SIZE) - 1;
  80. return bh;
  81. }
  82. if (offset < bh->b_size) {
  83. continue;
  84. }
  85. brelse(bh);
  86. bh = NULL;
  87. offset = 0;
  88. blkofs++;
  89. }
  90. brelse(bh);
  91. *res_dir = NULL;
  92. return NULL;
  93. }
  94. struct dentry * qnx4_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
  95. {
  96. int ino;
  97. struct qnx4_inode_entry *de;
  98. struct qnx4_link_info *lnk;
  99. struct buffer_head *bh;
  100. const char *name = dentry->d_name.name;
  101. int len = dentry->d_name.len;
  102. struct inode *foundinode = NULL;
  103. if (!(bh = qnx4_find_entry(len, dir, name, &de, &ino)))
  104. goto out;
  105. /* The entry is linked, let's get the real info */
  106. if ((de->di_status & QNX4_FILE_LINK) == QNX4_FILE_LINK) {
  107. lnk = (struct qnx4_link_info *) de;
  108. ino = (le32_to_cpu(lnk->dl_inode_blk) - 1) *
  109. QNX4_INODES_PER_BLOCK +
  110. lnk->dl_inode_ndx;
  111. }
  112. brelse(bh);
  113. foundinode = qnx4_iget(dir->i_sb, ino);
  114. if (IS_ERR(foundinode)) {
  115. QNX4DEBUG((KERN_ERR "qnx4: lookup->iget -> error %ld\n",
  116. PTR_ERR(foundinode)));
  117. return ERR_CAST(foundinode);
  118. }
  119. out:
  120. d_add(dentry, foundinode);
  121. return NULL;
  122. }