dir.c 3.7 KB

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