namei.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. thislen = strlen( de->di_fname );
  39. if ( thislen > namelen )
  40. thislen = namelen;
  41. if (len != thislen) {
  42. return 0;
  43. }
  44. if (strncmp(name, de->di_fname, len) == 0) {
  45. if ((de->di_status & (QNX4_FILE_USED|QNX4_FILE_LINK)) != 0) {
  46. return 1;
  47. }
  48. }
  49. return 0;
  50. }
  51. static struct buffer_head *qnx4_find_entry(int len, struct inode *dir,
  52. const char *name, struct qnx4_inode_entry **res_dir, int *ino)
  53. {
  54. unsigned long block, offset, blkofs;
  55. struct buffer_head *bh;
  56. *res_dir = NULL;
  57. if (!dir->i_sb) {
  58. printk(KERN_WARNING "qnx4: no superblock on dir.\n");
  59. return NULL;
  60. }
  61. bh = NULL;
  62. block = offset = blkofs = 0;
  63. while (blkofs * QNX4_BLOCK_SIZE + offset < dir->i_size) {
  64. if (!bh) {
  65. block = qnx4_block_map(dir, blkofs);
  66. if (block)
  67. bh = sb_bread(dir->i_sb, block);
  68. if (!bh) {
  69. blkofs++;
  70. continue;
  71. }
  72. }
  73. *res_dir = (struct qnx4_inode_entry *) (bh->b_data + offset);
  74. if (qnx4_match(len, name, bh, &offset)) {
  75. *ino = block * QNX4_INODES_PER_BLOCK +
  76. (offset / QNX4_DIR_ENTRY_SIZE) - 1;
  77. return bh;
  78. }
  79. if (offset < bh->b_size) {
  80. continue;
  81. }
  82. brelse(bh);
  83. bh = NULL;
  84. offset = 0;
  85. blkofs++;
  86. }
  87. brelse(bh);
  88. *res_dir = NULL;
  89. return NULL;
  90. }
  91. struct dentry * qnx4_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
  92. {
  93. int ino;
  94. struct qnx4_inode_entry *de;
  95. struct qnx4_link_info *lnk;
  96. struct buffer_head *bh;
  97. const char *name = dentry->d_name.name;
  98. int len = dentry->d_name.len;
  99. struct inode *foundinode = NULL;
  100. if (!(bh = qnx4_find_entry(len, dir, name, &de, &ino)))
  101. goto out;
  102. /* The entry is linked, let's get the real info */
  103. if ((de->di_status & QNX4_FILE_LINK) == QNX4_FILE_LINK) {
  104. lnk = (struct qnx4_link_info *) de;
  105. ino = (le32_to_cpu(lnk->dl_inode_blk) - 1) *
  106. QNX4_INODES_PER_BLOCK +
  107. lnk->dl_inode_ndx;
  108. }
  109. brelse(bh);
  110. foundinode = qnx4_iget(dir->i_sb, ino);
  111. if (IS_ERR(foundinode)) {
  112. QNX4DEBUG((KERN_ERR "qnx4: lookup->iget -> error %ld\n",
  113. PTR_ERR(foundinode)));
  114. return ERR_CAST(foundinode);
  115. }
  116. out:
  117. d_add(dentry, foundinode);
  118. return NULL;
  119. }