namei.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. bh = NULL;
  58. block = offset = blkofs = 0;
  59. while (blkofs * QNX4_BLOCK_SIZE + offset < dir->i_size) {
  60. if (!bh) {
  61. block = qnx4_block_map(dir, blkofs);
  62. if (block)
  63. bh = sb_bread(dir->i_sb, block);
  64. if (!bh) {
  65. blkofs++;
  66. continue;
  67. }
  68. }
  69. *res_dir = (struct qnx4_inode_entry *) (bh->b_data + offset);
  70. if (qnx4_match(len, name, bh, &offset)) {
  71. *ino = block * QNX4_INODES_PER_BLOCK +
  72. (offset / QNX4_DIR_ENTRY_SIZE) - 1;
  73. return bh;
  74. }
  75. if (offset < bh->b_size) {
  76. continue;
  77. }
  78. brelse(bh);
  79. bh = NULL;
  80. offset = 0;
  81. blkofs++;
  82. }
  83. brelse(bh);
  84. *res_dir = NULL;
  85. return NULL;
  86. }
  87. struct dentry * qnx4_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
  88. {
  89. int ino;
  90. struct qnx4_inode_entry *de;
  91. struct qnx4_link_info *lnk;
  92. struct buffer_head *bh;
  93. const char *name = dentry->d_name.name;
  94. int len = dentry->d_name.len;
  95. struct inode *foundinode = NULL;
  96. if (!(bh = qnx4_find_entry(len, dir, name, &de, &ino)))
  97. goto out;
  98. /* The entry is linked, let's get the real info */
  99. if ((de->di_status & QNX4_FILE_LINK) == QNX4_FILE_LINK) {
  100. lnk = (struct qnx4_link_info *) de;
  101. ino = (le32_to_cpu(lnk->dl_inode_blk) - 1) *
  102. QNX4_INODES_PER_BLOCK +
  103. lnk->dl_inode_ndx;
  104. }
  105. brelse(bh);
  106. foundinode = qnx4_iget(dir->i_sb, ino);
  107. if (IS_ERR(foundinode)) {
  108. QNX4DEBUG((KERN_ERR "qnx4: lookup->iget -> error %ld\n",
  109. PTR_ERR(foundinode)));
  110. return ERR_CAST(foundinode);
  111. }
  112. out:
  113. d_add(dentry, foundinode);
  114. return NULL;
  115. }