dir.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. * 28-05-1998 by Richard Frowijn : first release.
  12. * 20-06-1998 by Frank Denis : Linux 2.1.99+ & dcache support.
  13. */
  14. #include <linux/buffer_head.h>
  15. #include "qnx4.h"
  16. /*
  17. * A qnx4 directory entry is an inode entry or link info
  18. * depending on the status field in the last byte. The
  19. * first byte is where the name start either way, and a
  20. * zero means it's empty.
  21. *
  22. * Also, due to a bug in gcc, we don't want to use the
  23. * real (differently sized) name arrays in the inode and
  24. * link entries, but always the 'de_name[]' one in the
  25. * fake struct entry.
  26. *
  27. * See
  28. *
  29. * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99578#c6
  30. *
  31. * for details, but basically gcc will take the size of the
  32. * 'name' array from one of the used union entries randomly.
  33. *
  34. * This use of 'de_name[]' (48 bytes) avoids the false positive
  35. * warnings that would happen if gcc decides to use 'inode.di_name'
  36. * (16 bytes) even when the pointer and size were to come from
  37. * 'link.dl_name' (48 bytes).
  38. *
  39. * In all cases the actual name pointer itself is the same, it's
  40. * only the gcc internal 'what is the size of this field' logic
  41. * that can get confused.
  42. */
  43. union qnx4_directory_entry {
  44. struct {
  45. const char de_name[48];
  46. u8 de_pad[15];
  47. u8 de_status;
  48. };
  49. struct qnx4_inode_entry inode;
  50. struct qnx4_link_info link;
  51. };
  52. static int qnx4_readdir(struct file *file, struct dir_context *ctx)
  53. {
  54. struct inode *inode = file_inode(file);
  55. unsigned int offset;
  56. struct buffer_head *bh;
  57. unsigned long blknum;
  58. int ix, ino;
  59. int size;
  60. QNX4DEBUG((KERN_INFO "qnx4_readdir:i_size = %ld\n", (long) inode->i_size));
  61. QNX4DEBUG((KERN_INFO "pos = %ld\n", (long) ctx->pos));
  62. while (ctx->pos < inode->i_size) {
  63. blknum = qnx4_block_map(inode, ctx->pos >> QNX4_BLOCK_SIZE_BITS);
  64. bh = sb_bread(inode->i_sb, blknum);
  65. if (bh == NULL) {
  66. printk(KERN_ERR "qnx4_readdir: bread failed (%ld)\n", blknum);
  67. return 0;
  68. }
  69. ix = (ctx->pos >> QNX4_DIR_ENTRY_SIZE_BITS) % QNX4_INODES_PER_BLOCK;
  70. for (; ix < QNX4_INODES_PER_BLOCK; ix++, ctx->pos += QNX4_DIR_ENTRY_SIZE) {
  71. union qnx4_directory_entry *de;
  72. offset = ix * QNX4_DIR_ENTRY_SIZE;
  73. de = (union qnx4_directory_entry *) (bh->b_data + offset);
  74. if (!de->de_name[0])
  75. continue;
  76. if (!(de->de_status & (QNX4_FILE_USED|QNX4_FILE_LINK)))
  77. continue;
  78. if (!(de->de_status & QNX4_FILE_LINK)) {
  79. size = sizeof(de->inode.di_fname);
  80. ino = blknum * QNX4_INODES_PER_BLOCK + ix - 1;
  81. } else {
  82. size = sizeof(de->link.dl_fname);
  83. ino = ( le32_to_cpu(de->link.dl_inode_blk) - 1 ) *
  84. QNX4_INODES_PER_BLOCK +
  85. de->link.dl_inode_ndx;
  86. }
  87. size = strnlen(de->de_name, size);
  88. QNX4DEBUG((KERN_INFO "qnx4_readdir:%.*s\n", size, name));
  89. if (!dir_emit(ctx, de->de_name, size, ino, DT_UNKNOWN)) {
  90. brelse(bh);
  91. return 0;
  92. }
  93. }
  94. brelse(bh);
  95. }
  96. return 0;
  97. }
  98. const struct file_operations qnx4_dir_operations =
  99. {
  100. .llseek = generic_file_llseek,
  101. .read = generic_read_dir,
  102. .iterate_shared = qnx4_readdir,
  103. .fsync = generic_file_fsync,
  104. };
  105. const struct inode_operations qnx4_dir_inode_operations =
  106. {
  107. .lookup = qnx4_lookup,
  108. };