namei.c 3.3 KB

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