dir.c 4.1 KB

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