dir.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/fs/affs/dir.c
  4. *
  5. * (c) 1996 Hans-Joachim Widmaier - Rewritten
  6. *
  7. * (C) 1993 Ray Burr - Modified for Amiga FFS filesystem.
  8. *
  9. * (C) 1992 Eric Youngdale Modified for ISO 9660 filesystem.
  10. *
  11. * (C) 1991 Linus Torvalds - minix filesystem
  12. *
  13. * affs directory handling functions
  14. *
  15. */
  16. #include <linux/iversion.h>
  17. #include "affs.h"
  18. static int affs_readdir(struct file *, struct dir_context *);
  19. const struct file_operations affs_dir_operations = {
  20. .read = generic_read_dir,
  21. .llseek = generic_file_llseek,
  22. .iterate_shared = affs_readdir,
  23. .fsync = affs_file_fsync,
  24. };
  25. /*
  26. * directories can handle most operations...
  27. */
  28. const struct inode_operations affs_dir_inode_operations = {
  29. .create = affs_create,
  30. .lookup = affs_lookup,
  31. .link = affs_link,
  32. .unlink = affs_unlink,
  33. .symlink = affs_symlink,
  34. .mkdir = affs_mkdir,
  35. .rmdir = affs_rmdir,
  36. .rename = affs_rename2,
  37. .setattr = affs_notify_change,
  38. };
  39. static int
  40. affs_readdir(struct file *file, struct dir_context *ctx)
  41. {
  42. struct inode *inode = file_inode(file);
  43. struct super_block *sb = inode->i_sb;
  44. struct buffer_head *dir_bh = NULL;
  45. struct buffer_head *fh_bh = NULL;
  46. unsigned char *name;
  47. int namelen;
  48. u32 i;
  49. int hash_pos;
  50. int chain_pos;
  51. u32 ino;
  52. int error = 0;
  53. pr_debug("%s(ino=%lu,f_pos=%llx)\n", __func__, inode->i_ino, ctx->pos);
  54. if (ctx->pos < 2) {
  55. file->private_data = (void *)0;
  56. if (!dir_emit_dots(file, ctx))
  57. return 0;
  58. }
  59. affs_lock_dir(inode);
  60. chain_pos = (ctx->pos - 2) & 0xffff;
  61. hash_pos = (ctx->pos - 2) >> 16;
  62. if (chain_pos == 0xffff) {
  63. affs_warning(sb, "readdir", "More than 65535 entries in chain");
  64. chain_pos = 0;
  65. hash_pos++;
  66. ctx->pos = ((hash_pos << 16) | chain_pos) + 2;
  67. }
  68. dir_bh = affs_bread(sb, inode->i_ino);
  69. if (!dir_bh)
  70. goto out_unlock_dir;
  71. /* If the directory hasn't changed since the last call to readdir(),
  72. * we can jump directly to where we left off.
  73. */
  74. ino = (u32)(long)file->private_data;
  75. if (ino && inode_eq_iversion(inode, file->f_version)) {
  76. pr_debug("readdir() left off=%d\n", ino);
  77. goto inside;
  78. }
  79. ino = be32_to_cpu(AFFS_HEAD(dir_bh)->table[hash_pos]);
  80. for (i = 0; ino && i < chain_pos; i++) {
  81. fh_bh = affs_bread(sb, ino);
  82. if (!fh_bh) {
  83. affs_error(sb, "readdir","Cannot read block %d", i);
  84. error = -EIO;
  85. goto out_brelse_dir;
  86. }
  87. ino = be32_to_cpu(AFFS_TAIL(sb, fh_bh)->hash_chain);
  88. affs_brelse(fh_bh);
  89. fh_bh = NULL;
  90. }
  91. if (ino)
  92. goto inside;
  93. hash_pos++;
  94. for (; hash_pos < AFFS_SB(sb)->s_hashsize; hash_pos++) {
  95. ino = be32_to_cpu(AFFS_HEAD(dir_bh)->table[hash_pos]);
  96. if (!ino)
  97. continue;
  98. ctx->pos = (hash_pos << 16) + 2;
  99. inside:
  100. do {
  101. fh_bh = affs_bread(sb, ino);
  102. if (!fh_bh) {
  103. affs_error(sb, "readdir",
  104. "Cannot read block %d", ino);
  105. break;
  106. }
  107. namelen = min(AFFS_TAIL(sb, fh_bh)->name[0],
  108. (u8)AFFSNAMEMAX);
  109. name = AFFS_TAIL(sb, fh_bh)->name + 1;
  110. pr_debug("readdir(): dir_emit(\"%.*s\", ino=%u), hash=%d, f_pos=%llx\n",
  111. namelen, name, ino, hash_pos, ctx->pos);
  112. if (!dir_emit(ctx, name, namelen, ino, DT_UNKNOWN))
  113. goto done;
  114. ctx->pos++;
  115. ino = be32_to_cpu(AFFS_TAIL(sb, fh_bh)->hash_chain);
  116. affs_brelse(fh_bh);
  117. fh_bh = NULL;
  118. } while (ino);
  119. }
  120. done:
  121. file->f_version = inode_query_iversion(inode);
  122. file->private_data = (void *)(long)ino;
  123. affs_brelse(fh_bh);
  124. out_brelse_dir:
  125. affs_brelse(dir_bh);
  126. out_unlock_dir:
  127. affs_unlock_dir(inode);
  128. return error;
  129. }