dir.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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/mm.h>
  26. #include <linux/slab.h>
  27. #include <linux/bio.h>
  28. #include <linux/iversion.h>
  29. #include "udf_i.h"
  30. #include "udf_sb.h"
  31. static int udf_readdir(struct file *file, struct dir_context *ctx)
  32. {
  33. struct inode *dir = file_inode(file);
  34. loff_t nf_pos, emit_pos = 0;
  35. int flen;
  36. unsigned char *fname = NULL;
  37. int ret = 0;
  38. struct super_block *sb = dir->i_sb;
  39. bool pos_valid = false;
  40. struct udf_fileident_iter iter;
  41. if (ctx->pos == 0) {
  42. if (!dir_emit_dot(file, ctx))
  43. return 0;
  44. ctx->pos = 1;
  45. }
  46. nf_pos = (ctx->pos - 1) << 2;
  47. if (nf_pos >= dir->i_size)
  48. goto out;
  49. /*
  50. * Something changed since last readdir (either lseek was called or dir
  51. * changed)? We need to verify the position correctly points at the
  52. * beginning of some dir entry so that the directory parsing code does
  53. * not get confused. Since UDF does not have any reliable way of
  54. * identifying beginning of dir entry (names are under user control),
  55. * we need to scan the directory from the beginning.
  56. */
  57. if (!inode_eq_iversion(dir, *(u64 *)file->private_data)) {
  58. emit_pos = nf_pos;
  59. nf_pos = 0;
  60. } else {
  61. pos_valid = true;
  62. }
  63. fname = kmalloc(UDF_NAME_LEN, GFP_KERNEL);
  64. if (!fname) {
  65. ret = -ENOMEM;
  66. goto out;
  67. }
  68. for (ret = udf_fiiter_init(&iter, dir, nf_pos);
  69. !ret && iter.pos < dir->i_size;
  70. ret = udf_fiiter_advance(&iter)) {
  71. struct kernel_lb_addr tloc;
  72. udf_pblk_t iblock;
  73. /* Still not at offset where user asked us to read from? */
  74. if (iter.pos < emit_pos)
  75. continue;
  76. /* Update file position only if we got past the current one */
  77. pos_valid = true;
  78. ctx->pos = (iter.pos >> 2) + 1;
  79. if (iter.fi.fileCharacteristics & FID_FILE_CHAR_DELETED) {
  80. if (!UDF_QUERY_FLAG(sb, UDF_FLAG_UNDELETE))
  81. continue;
  82. }
  83. if (iter.fi.fileCharacteristics & FID_FILE_CHAR_HIDDEN) {
  84. if (!UDF_QUERY_FLAG(sb, UDF_FLAG_UNHIDE))
  85. continue;
  86. }
  87. if (iter.fi.fileCharacteristics & FID_FILE_CHAR_PARENT) {
  88. if (!dir_emit_dotdot(file, ctx))
  89. goto out_iter;
  90. continue;
  91. }
  92. flen = udf_get_filename(sb, iter.name,
  93. iter.fi.lengthFileIdent, fname, UDF_NAME_LEN);
  94. if (flen < 0)
  95. continue;
  96. tloc = lelb_to_cpu(iter.fi.icb.extLocation);
  97. iblock = udf_get_lb_pblock(sb, &tloc, 0);
  98. if (!dir_emit(ctx, fname, flen, iblock, DT_UNKNOWN))
  99. goto out_iter;
  100. }
  101. if (!ret) {
  102. ctx->pos = (iter.pos >> 2) + 1;
  103. pos_valid = true;
  104. }
  105. out_iter:
  106. udf_fiiter_release(&iter);
  107. out:
  108. if (pos_valid)
  109. *(u64 *)file->private_data = inode_query_iversion(dir);
  110. kfree(fname);
  111. return ret;
  112. }
  113. static int udf_dir_open(struct inode *inode, struct file *file)
  114. {
  115. file->private_data = kzalloc(sizeof(u64), GFP_KERNEL);
  116. if (!file->private_data)
  117. return -ENOMEM;
  118. return 0;
  119. }
  120. static int udf_dir_release(struct inode *inode, struct file *file)
  121. {
  122. kfree(file->private_data);
  123. return 0;
  124. }
  125. static loff_t udf_dir_llseek(struct file *file, loff_t offset, int whence)
  126. {
  127. return generic_llseek_cookie(file, offset, whence,
  128. (u64 *)file->private_data);
  129. }
  130. /* readdir and lookup functions */
  131. const struct file_operations udf_dir_operations = {
  132. .open = udf_dir_open,
  133. .release = udf_dir_release,
  134. .llseek = udf_dir_llseek,
  135. .read = generic_read_dir,
  136. .iterate_shared = udf_readdir,
  137. .unlocked_ioctl = udf_ioctl,
  138. .fsync = generic_file_fsync,
  139. };