dir.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * dir.c
  4. *
  5. * PURPOSE
  6. * Directory handling routines for the OSTA-UDF(tm) filesystem.
  7. *
  8. * COPYRIGHT
  9. * (C) 1998-2004 Ben Fennema
  10. *
  11. * HISTORY
  12. *
  13. * 10/05/98 dgb Split directory operations into its own file
  14. * Implemented directory reads via do_udf_readdir
  15. * 10/06/98 Made directory operations work!
  16. * 11/17/98 Rewrote directory to support ICBTAG_FLAG_AD_LONG
  17. * 11/25/98 blf Rewrote directory handling (readdir+lookup) to support reading
  18. * across blocks.
  19. * 12/12/98 Split out the lookup code to namei.c. bulk of directory
  20. * code now in directory.c:udf_fileident_read.
  21. */
  22. #include "udfdecl.h"
  23. #include <linux/string.h>
  24. #include <linux/errno.h>
  25. #include <linux/filelock.h>
  26. #include <linux/mm.h>
  27. #include <linux/slab.h>
  28. #include <linux/bio.h>
  29. #include <linux/iversion.h>
  30. #include "udf_i.h"
  31. #include "udf_sb.h"
  32. static int udf_readdir(struct file *file, struct dir_context *ctx)
  33. {
  34. struct inode *dir = file_inode(file);
  35. loff_t nf_pos, emit_pos = 0;
  36. int flen;
  37. unsigned char *fname = NULL;
  38. int ret = 0;
  39. struct super_block *sb = dir->i_sb;
  40. bool pos_valid = false;
  41. struct udf_fileident_iter iter;
  42. if (ctx->pos == 0) {
  43. if (!dir_emit_dot(file, ctx))
  44. return 0;
  45. ctx->pos = 1;
  46. }
  47. nf_pos = (ctx->pos - 1) << 2;
  48. if (nf_pos >= dir->i_size)
  49. goto out;
  50. /*
  51. * Something changed since last readdir (either lseek was called or dir
  52. * changed)? We need to verify the position correctly points at the
  53. * beginning of some dir entry so that the directory parsing code does
  54. * not get confused. Since UDF does not have any reliable way of
  55. * identifying beginning of dir entry (names are under user control),
  56. * we need to scan the directory from the beginning.
  57. */
  58. if (!inode_eq_iversion(dir, *(u64 *)file->private_data)) {
  59. emit_pos = nf_pos;
  60. nf_pos = 0;
  61. } else {
  62. pos_valid = true;
  63. }
  64. fname = kmalloc(UDF_NAME_LEN, GFP_KERNEL);
  65. if (!fname) {
  66. ret = -ENOMEM;
  67. goto out;
  68. }
  69. for (ret = udf_fiiter_init(&iter, dir, nf_pos);
  70. !ret && iter.pos < dir->i_size;
  71. ret = udf_fiiter_advance(&iter)) {
  72. struct kernel_lb_addr tloc;
  73. udf_pblk_t iblock;
  74. /* Still not at offset where user asked us to read from? */
  75. if (iter.pos < emit_pos)
  76. continue;
  77. /* Update file position only if we got past the current one */
  78. pos_valid = true;
  79. ctx->pos = (iter.pos >> 2) + 1;
  80. if (iter.fi.fileCharacteristics & FID_FILE_CHAR_DELETED) {
  81. if (!UDF_QUERY_FLAG(sb, UDF_FLAG_UNDELETE))
  82. continue;
  83. }
  84. if (iter.fi.fileCharacteristics & FID_FILE_CHAR_HIDDEN) {
  85. if (!UDF_QUERY_FLAG(sb, UDF_FLAG_UNHIDE))
  86. continue;
  87. }
  88. if (iter.fi.fileCharacteristics & FID_FILE_CHAR_PARENT) {
  89. if (!dir_emit_dotdot(file, ctx))
  90. goto out_iter;
  91. continue;
  92. }
  93. flen = udf_get_filename(sb, iter.name,
  94. iter.fi.lengthFileIdent, fname, UDF_NAME_LEN);
  95. if (flen < 0)
  96. continue;
  97. tloc = lelb_to_cpu(iter.fi.icb.extLocation);
  98. iblock = udf_get_lb_pblock(sb, &tloc, 0);
  99. if (!dir_emit(ctx, fname, flen, iblock, DT_UNKNOWN))
  100. goto out_iter;
  101. }
  102. if (!ret) {
  103. ctx->pos = (iter.pos >> 2) + 1;
  104. pos_valid = true;
  105. }
  106. out_iter:
  107. udf_fiiter_release(&iter);
  108. out:
  109. if (pos_valid)
  110. *(u64 *)file->private_data = inode_query_iversion(dir);
  111. kfree(fname);
  112. return ret;
  113. }
  114. static int udf_dir_open(struct inode *inode, struct file *file)
  115. {
  116. file->private_data = kzalloc(sizeof(u64), GFP_KERNEL);
  117. if (!file->private_data)
  118. return -ENOMEM;
  119. return 0;
  120. }
  121. static int udf_dir_release(struct inode *inode, struct file *file)
  122. {
  123. kfree(file->private_data);
  124. return 0;
  125. }
  126. static loff_t udf_dir_llseek(struct file *file, loff_t offset, int whence)
  127. {
  128. return generic_llseek_cookie(file, offset, whence,
  129. (u64 *)file->private_data);
  130. }
  131. /* readdir and lookup functions */
  132. const struct file_operations udf_dir_operations = {
  133. .open = udf_dir_open,
  134. .release = udf_dir_release,
  135. .llseek = udf_dir_llseek,
  136. .read = generic_read_dir,
  137. .iterate_shared = udf_readdir,
  138. .unlocked_ioctl = udf_ioctl,
  139. .fsync = generic_file_fsync,
  140. .setlease = generic_setlease,
  141. };