dir.c 3.3 KB

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