dir.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/fs/affs/dir.c
  4. *
  5. * (c) 1996 Hans-Joachim Widmaier - Rewritten
  6. *
  7. * (C) 1993 Ray Burr - Modified for Amiga FFS filesystem.
  8. *
  9. * (C) 1992 Eric Youngdale Modified for ISO 9660 filesystem.
  10. *
  11. * (C) 1991 Linus Torvalds - minix filesystem
  12. *
  13. * affs directory handling functions
  14. *
  15. */
  16. #include <linux/iversion.h>
  17. #include "affs.h"
  18. struct affs_dir_data {
  19. unsigned long ino;
  20. u64 cookie;
  21. };
  22. static int affs_readdir(struct file *, struct dir_context *);
  23. static loff_t affs_dir_llseek(struct file *file, loff_t offset, int whence)
  24. {
  25. struct affs_dir_data *data = file->private_data;
  26. return generic_llseek_cookie(file, offset, whence, &data->cookie);
  27. }
  28. static int affs_dir_open(struct inode *inode, struct file *file)
  29. {
  30. struct affs_dir_data *data;
  31. data = kzalloc(sizeof(struct affs_dir_data), GFP_KERNEL);
  32. if (!data)
  33. return -ENOMEM;
  34. file->private_data = data;
  35. return 0;
  36. }
  37. static int affs_dir_release(struct inode *inode, struct file *file)
  38. {
  39. kfree(file->private_data);
  40. return 0;
  41. }
  42. const struct file_operations affs_dir_operations = {
  43. .open = affs_dir_open,
  44. .read = generic_read_dir,
  45. .llseek = affs_dir_llseek,
  46. .iterate_shared = affs_readdir,
  47. .fsync = affs_file_fsync,
  48. .release = affs_dir_release,
  49. };
  50. /*
  51. * directories can handle most operations...
  52. */
  53. const struct inode_operations affs_dir_inode_operations = {
  54. .create = affs_create,
  55. .lookup = affs_lookup,
  56. .link = affs_link,
  57. .unlink = affs_unlink,
  58. .symlink = affs_symlink,
  59. .mkdir = affs_mkdir,
  60. .rmdir = affs_rmdir,
  61. .rename = affs_rename2,
  62. .setattr = affs_notify_change,
  63. };
  64. static int
  65. affs_readdir(struct file *file, struct dir_context *ctx)
  66. {
  67. struct inode *inode = file_inode(file);
  68. struct affs_dir_data *data = file->private_data;
  69. struct super_block *sb = inode->i_sb;
  70. struct buffer_head *dir_bh = NULL;
  71. struct buffer_head *fh_bh = NULL;
  72. unsigned char *name;
  73. int namelen;
  74. u32 i;
  75. int hash_pos;
  76. int chain_pos;
  77. u32 ino;
  78. int error = 0;
  79. pr_debug("%s(ino=%lu,f_pos=%llx)\n", __func__, inode->i_ino, ctx->pos);
  80. if (ctx->pos < 2) {
  81. data->ino = 0;
  82. if (!dir_emit_dots(file, ctx))
  83. return 0;
  84. }
  85. affs_lock_dir(inode);
  86. chain_pos = (ctx->pos - 2) & 0xffff;
  87. hash_pos = (ctx->pos - 2) >> 16;
  88. if (chain_pos == 0xffff) {
  89. affs_warning(sb, "readdir", "More than 65535 entries in chain");
  90. chain_pos = 0;
  91. hash_pos++;
  92. ctx->pos = ((hash_pos << 16) | chain_pos) + 2;
  93. }
  94. dir_bh = affs_bread(sb, inode->i_ino);
  95. if (!dir_bh)
  96. goto out_unlock_dir;
  97. /* If the directory hasn't changed since the last call to readdir(),
  98. * we can jump directly to where we left off.
  99. */
  100. ino = data->ino;
  101. if (ino && inode_eq_iversion(inode, data->cookie)) {
  102. pr_debug("readdir() left off=%d\n", ino);
  103. goto inside;
  104. }
  105. ino = be32_to_cpu(AFFS_HEAD(dir_bh)->table[hash_pos]);
  106. for (i = 0; ino && i < chain_pos; i++) {
  107. fh_bh = affs_bread(sb, ino);
  108. if (!fh_bh) {
  109. affs_error(sb, "readdir","Cannot read block %d", i);
  110. error = -EIO;
  111. goto out_brelse_dir;
  112. }
  113. ino = be32_to_cpu(AFFS_TAIL(sb, fh_bh)->hash_chain);
  114. affs_brelse(fh_bh);
  115. fh_bh = NULL;
  116. }
  117. if (ino)
  118. goto inside;
  119. hash_pos++;
  120. for (; hash_pos < AFFS_SB(sb)->s_hashsize; hash_pos++) {
  121. ino = be32_to_cpu(AFFS_HEAD(dir_bh)->table[hash_pos]);
  122. if (!ino)
  123. continue;
  124. ctx->pos = (hash_pos << 16) + 2;
  125. inside:
  126. do {
  127. fh_bh = affs_bread(sb, ino);
  128. if (!fh_bh) {
  129. affs_error(sb, "readdir",
  130. "Cannot read block %d", ino);
  131. break;
  132. }
  133. namelen = min(AFFS_TAIL(sb, fh_bh)->name[0],
  134. (u8)AFFSNAMEMAX);
  135. name = AFFS_TAIL(sb, fh_bh)->name + 1;
  136. pr_debug("readdir(): dir_emit(\"%.*s\", ino=%u), hash=%d, f_pos=%llx\n",
  137. namelen, name, ino, hash_pos, ctx->pos);
  138. if (!dir_emit(ctx, name, namelen, ino, DT_UNKNOWN))
  139. goto done;
  140. ctx->pos++;
  141. ino = be32_to_cpu(AFFS_TAIL(sb, fh_bh)->hash_chain);
  142. affs_brelse(fh_bh);
  143. fh_bh = NULL;
  144. } while (ino);
  145. }
  146. done:
  147. data->cookie = inode_query_iversion(inode);
  148. data->ino = ino;
  149. affs_brelse(fh_bh);
  150. out_brelse_dir:
  151. affs_brelse(dir_bh);
  152. out_unlock_dir:
  153. affs_unlock_dir(inode);
  154. return error;
  155. }