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