dir.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 *, void *, filldir_t);
  17. const struct file_operations affs_dir_operations = {
  18. .read = generic_read_dir,
  19. .llseek = generic_file_llseek,
  20. .readdir = 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 *filp, void *dirent, filldir_t filldir)
  39. {
  40. struct inode *inode = filp->f_path.dentry->d_inode;
  41. struct super_block *sb = inode->i_sb;
  42. struct buffer_head *dir_bh;
  43. struct buffer_head *fh_bh;
  44. unsigned char *name;
  45. int namelen;
  46. u32 i;
  47. int hash_pos;
  48. int chain_pos;
  49. u32 f_pos;
  50. u32 ino;
  51. int stored;
  52. int res;
  53. pr_debug("AFFS: readdir(ino=%lu,f_pos=%lx)\n",inode->i_ino,(unsigned long)filp->f_pos);
  54. stored = 0;
  55. res = -EIO;
  56. dir_bh = NULL;
  57. fh_bh = NULL;
  58. f_pos = filp->f_pos;
  59. if (f_pos == 0) {
  60. filp->private_data = (void *)0;
  61. if (filldir(dirent, ".", 1, f_pos, inode->i_ino, DT_DIR) < 0)
  62. return 0;
  63. filp->f_pos = f_pos = 1;
  64. stored++;
  65. }
  66. if (f_pos == 1) {
  67. if (filldir(dirent, "..", 2, f_pos, parent_ino(filp->f_path.dentry), DT_DIR) < 0)
  68. return stored;
  69. filp->f_pos = f_pos = 2;
  70. stored++;
  71. }
  72. affs_lock_dir(inode);
  73. chain_pos = (f_pos - 2) & 0xffff;
  74. hash_pos = (f_pos - 2) >> 16;
  75. if (chain_pos == 0xffff) {
  76. affs_warning(sb, "readdir", "More than 65535 entries in chain");
  77. chain_pos = 0;
  78. hash_pos++;
  79. filp->f_pos = ((hash_pos << 16) | chain_pos) + 2;
  80. }
  81. dir_bh = affs_bread(sb, inode->i_ino);
  82. if (!dir_bh)
  83. goto readdir_out;
  84. /* If the directory hasn't changed since the last call to readdir(),
  85. * we can jump directly to where we left off.
  86. */
  87. ino = (u32)(long)filp->private_data;
  88. if (ino && filp->f_version == inode->i_version) {
  89. pr_debug("AFFS: readdir() left off=%d\n", ino);
  90. goto inside;
  91. }
  92. ino = be32_to_cpu(AFFS_HEAD(dir_bh)->table[hash_pos]);
  93. for (i = 0; ino && i < chain_pos; i++) {
  94. fh_bh = affs_bread(sb, ino);
  95. if (!fh_bh) {
  96. affs_error(sb, "readdir","Cannot read block %d", i);
  97. goto readdir_out;
  98. }
  99. ino = be32_to_cpu(AFFS_TAIL(sb, fh_bh)->hash_chain);
  100. affs_brelse(fh_bh);
  101. fh_bh = NULL;
  102. }
  103. if (ino)
  104. goto inside;
  105. hash_pos++;
  106. for (; hash_pos < AFFS_SB(sb)->s_hashsize; hash_pos++) {
  107. ino = be32_to_cpu(AFFS_HEAD(dir_bh)->table[hash_pos]);
  108. if (!ino)
  109. continue;
  110. f_pos = (hash_pos << 16) + 2;
  111. inside:
  112. do {
  113. fh_bh = affs_bread(sb, ino);
  114. if (!fh_bh) {
  115. affs_error(sb, "readdir","Cannot read block %d", ino);
  116. goto readdir_done;
  117. }
  118. namelen = min(AFFS_TAIL(sb, fh_bh)->name[0], (u8)30);
  119. name = AFFS_TAIL(sb, fh_bh)->name + 1;
  120. pr_debug("AFFS: readdir(): filldir(\"%.*s\", ino=%u), hash=%d, f_pos=%x\n",
  121. namelen, name, ino, hash_pos, f_pos);
  122. if (filldir(dirent, name, namelen, f_pos, ino, DT_UNKNOWN) < 0)
  123. goto readdir_done;
  124. stored++;
  125. f_pos++;
  126. ino = be32_to_cpu(AFFS_TAIL(sb, fh_bh)->hash_chain);
  127. affs_brelse(fh_bh);
  128. fh_bh = NULL;
  129. } while (ino);
  130. }
  131. readdir_done:
  132. filp->f_pos = f_pos;
  133. filp->f_version = inode->i_version;
  134. filp->private_data = (void *)(long)ino;
  135. res = stored;
  136. readdir_out:
  137. affs_brelse(dir_bh);
  138. affs_brelse(fh_bh);
  139. affs_unlock_dir(inode);
  140. pr_debug("AFFS: readdir()=%d\n", stored);
  141. return res;
  142. }