dir.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. int error = 0;
  51. pr_debug("AFFS: readdir(ino=%lu,f_pos=%lx)\n",
  52. inode->i_ino, (unsigned long)ctx->pos);
  53. if (ctx->pos < 2) {
  54. file->private_data = (void *)0;
  55. if (!dir_emit_dots(file, ctx))
  56. return 0;
  57. }
  58. affs_lock_dir(inode);
  59. chain_pos = (ctx->pos - 2) & 0xffff;
  60. hash_pos = (ctx->pos - 2) >> 16;
  61. if (chain_pos == 0xffff) {
  62. affs_warning(sb, "readdir", "More than 65535 entries in chain");
  63. chain_pos = 0;
  64. hash_pos++;
  65. ctx->pos = ((hash_pos << 16) | chain_pos) + 2;
  66. }
  67. dir_bh = affs_bread(sb, inode->i_ino);
  68. if (!dir_bh)
  69. goto out_unlock_dir;
  70. /* If the directory hasn't changed since the last call to readdir(),
  71. * we can jump directly to where we left off.
  72. */
  73. ino = (u32)(long)file->private_data;
  74. if (ino && file->f_version == inode->i_version) {
  75. pr_debug("AFFS: readdir() left off=%d\n", ino);
  76. goto inside;
  77. }
  78. ino = be32_to_cpu(AFFS_HEAD(dir_bh)->table[hash_pos]);
  79. for (i = 0; ino && i < chain_pos; i++) {
  80. fh_bh = affs_bread(sb, ino);
  81. if (!fh_bh) {
  82. affs_error(sb, "readdir","Cannot read block %d", i);
  83. error = -EIO;
  84. goto out_brelse_dir;
  85. }
  86. ino = be32_to_cpu(AFFS_TAIL(sb, fh_bh)->hash_chain);
  87. affs_brelse(fh_bh);
  88. fh_bh = NULL;
  89. }
  90. if (ino)
  91. goto inside;
  92. hash_pos++;
  93. for (; hash_pos < AFFS_SB(sb)->s_hashsize; hash_pos++) {
  94. ino = be32_to_cpu(AFFS_HEAD(dir_bh)->table[hash_pos]);
  95. if (!ino)
  96. continue;
  97. ctx->pos = (hash_pos << 16) + 2;
  98. inside:
  99. do {
  100. fh_bh = affs_bread(sb, ino);
  101. if (!fh_bh) {
  102. affs_error(sb, "readdir",
  103. "Cannot read block %d", ino);
  104. break;
  105. }
  106. namelen = min(AFFS_TAIL(sb, fh_bh)->name[0], (u8)30);
  107. name = AFFS_TAIL(sb, fh_bh)->name + 1;
  108. pr_debug("AFFS: readdir(): dir_emit(\"%.*s\", "
  109. "ino=%u), hash=%d, f_pos=%x\n",
  110. namelen, name, ino, hash_pos, (u32)ctx->pos);
  111. if (!dir_emit(ctx, name, namelen, ino, DT_UNKNOWN))
  112. goto done;
  113. ctx->pos++;
  114. ino = be32_to_cpu(AFFS_TAIL(sb, fh_bh)->hash_chain);
  115. affs_brelse(fh_bh);
  116. fh_bh = NULL;
  117. } while (ino);
  118. }
  119. done:
  120. file->f_version = inode->i_version;
  121. file->private_data = (void *)(long)ino;
  122. affs_brelse(fh_bh);
  123. out_brelse_dir:
  124. affs_brelse(dir_bh);
  125. out_unlock_dir:
  126. affs_unlock_dir(inode);
  127. return error;
  128. }