namei.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * QNX4 file system, Linux implementation.
  4. *
  5. * Version : 0.2.1
  6. *
  7. * Using parts of the xiafs filesystem.
  8. *
  9. * History :
  10. *
  11. * 01-06-1998 by Richard Frowijn : first release.
  12. * 21-06-1998 by Frank Denis : dcache support, fixed error codes.
  13. * 04-07-1998 by Frank Denis : first step for rmdir/unlink.
  14. */
  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(KERN_WARNING "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. thislen = strlen( de->di_fname );
  40. if ( thislen > namelen )
  41. thislen = namelen;
  42. if (len != thislen) {
  43. return 0;
  44. }
  45. if (strncmp(name, de->di_fname, len) == 0) {
  46. if ((de->di_status & (QNX4_FILE_USED|QNX4_FILE_LINK)) != 0) {
  47. return 1;
  48. }
  49. }
  50. return 0;
  51. }
  52. static struct buffer_head *qnx4_find_entry(int len, struct inode *dir,
  53. const char *name, struct qnx4_inode_entry **res_dir, int *ino)
  54. {
  55. unsigned long block, offset, blkofs;
  56. struct buffer_head *bh;
  57. *res_dir = NULL;
  58. bh = NULL;
  59. block = offset = blkofs = 0;
  60. while (blkofs * QNX4_BLOCK_SIZE + offset < dir->i_size) {
  61. if (!bh) {
  62. block = qnx4_block_map(dir, blkofs);
  63. if (block)
  64. bh = sb_bread(dir->i_sb, block);
  65. if (!bh) {
  66. blkofs++;
  67. continue;
  68. }
  69. }
  70. *res_dir = (struct qnx4_inode_entry *) (bh->b_data + offset);
  71. if (qnx4_match(len, name, bh, &offset)) {
  72. *ino = block * QNX4_INODES_PER_BLOCK +
  73. (offset / QNX4_DIR_ENTRY_SIZE) - 1;
  74. return bh;
  75. }
  76. if (offset < bh->b_size) {
  77. continue;
  78. }
  79. brelse(bh);
  80. bh = NULL;
  81. offset = 0;
  82. blkofs++;
  83. }
  84. brelse(bh);
  85. *res_dir = NULL;
  86. return NULL;
  87. }
  88. struct dentry * qnx4_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
  89. {
  90. int ino;
  91. struct qnx4_inode_entry *de;
  92. struct qnx4_link_info *lnk;
  93. struct buffer_head *bh;
  94. const char *name = dentry->d_name.name;
  95. int len = dentry->d_name.len;
  96. struct inode *foundinode = NULL;
  97. if (!(bh = qnx4_find_entry(len, dir, name, &de, &ino)))
  98. goto out;
  99. /* The entry is linked, let's get the real info */
  100. if ((de->di_status & QNX4_FILE_LINK) == QNX4_FILE_LINK) {
  101. lnk = (struct qnx4_link_info *) de;
  102. ino = (le32_to_cpu(lnk->dl_inode_blk) - 1) *
  103. QNX4_INODES_PER_BLOCK +
  104. lnk->dl_inode_ndx;
  105. }
  106. brelse(bh);
  107. foundinode = qnx4_iget(dir->i_sb, ino);
  108. if (IS_ERR(foundinode))
  109. QNX4DEBUG((KERN_ERR "qnx4: lookup->iget -> error %ld\n",
  110. PTR_ERR(foundinode)));
  111. out:
  112. return d_splice_alias(foundinode, dentry);
  113. }