dir.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * dir.c
  3. *
  4. * PURPOSE
  5. * Directory handling routines for the OSTA-UDF(tm) filesystem.
  6. *
  7. * COPYRIGHT
  8. * This file is distributed under the terms of the GNU General Public
  9. * License (GPL). Copies of the GPL can be obtained from:
  10. * ftp://prep.ai.mit.edu/pub/gnu/GPL
  11. * Each contributing author retains all rights to their own work.
  12. *
  13. * (C) 1998-2004 Ben Fennema
  14. *
  15. * HISTORY
  16. *
  17. * 10/05/98 dgb Split directory operations into its own file
  18. * Implemented directory reads via do_udf_readdir
  19. * 10/06/98 Made directory operations work!
  20. * 11/17/98 Rewrote directory to support ICBTAG_FLAG_AD_LONG
  21. * 11/25/98 blf Rewrote directory handling (readdir+lookup) to support reading
  22. * across blocks.
  23. * 12/12/98 Split out the lookup code to namei.c. bulk of directory
  24. * code now in directory.c:udf_fileident_read.
  25. */
  26. #include "udfdecl.h"
  27. #include <linux/string.h>
  28. #include <linux/errno.h>
  29. #include <linux/mm.h>
  30. #include <linux/slab.h>
  31. #include <linux/bio.h>
  32. #include <linux/iversion.h>
  33. #include "udf_i.h"
  34. #include "udf_sb.h"
  35. static int udf_readdir(struct file *file, struct dir_context *ctx)
  36. {
  37. struct inode *dir = file_inode(file);
  38. struct udf_inode_info *iinfo = UDF_I(dir);
  39. struct udf_fileident_bh fibh = { .sbh = NULL, .ebh = NULL};
  40. struct fileIdentDesc *fi = NULL;
  41. struct fileIdentDesc cfi;
  42. udf_pblk_t block, iblock;
  43. loff_t nf_pos, emit_pos = 0;
  44. int flen;
  45. unsigned char *fname = NULL, *copy_name = NULL;
  46. unsigned char *nameptr;
  47. uint16_t liu;
  48. uint8_t lfi;
  49. loff_t size = udf_ext0_offset(dir) + dir->i_size;
  50. struct buffer_head *tmp, *bha[16];
  51. struct kernel_lb_addr eloc;
  52. uint32_t elen;
  53. sector_t offset;
  54. int i, num, ret = 0;
  55. struct extent_position epos = { NULL, 0, {0, 0} };
  56. struct super_block *sb = dir->i_sb;
  57. bool pos_valid = false;
  58. if (ctx->pos == 0) {
  59. if (!dir_emit_dot(file, ctx))
  60. return 0;
  61. ctx->pos = 1;
  62. }
  63. nf_pos = (ctx->pos - 1) << 2;
  64. if (nf_pos >= size)
  65. goto out;
  66. /*
  67. * Something changed since last readdir (either lseek was called or dir
  68. * changed)? We need to verify the position correctly points at the
  69. * beginning of some dir entry so that the directory parsing code does
  70. * not get confused. Since UDF does not have any reliable way of
  71. * identifying beginning of dir entry (names are under user control),
  72. * we need to scan the directory from the beginning.
  73. */
  74. if (!inode_eq_iversion(dir, file->f_version)) {
  75. emit_pos = nf_pos;
  76. nf_pos = 0;
  77. } else {
  78. pos_valid = true;
  79. }
  80. fname = kmalloc(UDF_NAME_LEN, GFP_NOFS);
  81. if (!fname) {
  82. ret = -ENOMEM;
  83. goto out;
  84. }
  85. if (nf_pos == 0)
  86. nf_pos = udf_ext0_offset(dir);
  87. fibh.soffset = fibh.eoffset = nf_pos & (sb->s_blocksize - 1);
  88. if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
  89. if (inode_bmap(dir, nf_pos >> sb->s_blocksize_bits,
  90. &epos, &eloc, &elen, &offset)
  91. != (EXT_RECORDED_ALLOCATED >> 30)) {
  92. ret = -ENOENT;
  93. goto out;
  94. }
  95. block = udf_get_lb_pblock(sb, &eloc, offset);
  96. if ((++offset << sb->s_blocksize_bits) < elen) {
  97. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
  98. epos.offset -= sizeof(struct short_ad);
  99. else if (iinfo->i_alloc_type ==
  100. ICBTAG_FLAG_AD_LONG)
  101. epos.offset -= sizeof(struct long_ad);
  102. } else {
  103. offset = 0;
  104. }
  105. if (!(fibh.sbh = fibh.ebh = udf_tread(sb, block))) {
  106. ret = -EIO;
  107. goto out;
  108. }
  109. if (!(offset & ((16 >> (sb->s_blocksize_bits - 9)) - 1))) {
  110. i = 16 >> (sb->s_blocksize_bits - 9);
  111. if (i + offset > (elen >> sb->s_blocksize_bits))
  112. i = (elen >> sb->s_blocksize_bits) - offset;
  113. for (num = 0; i > 0; i--) {
  114. block = udf_get_lb_pblock(sb, &eloc, offset + i);
  115. tmp = udf_tgetblk(sb, block);
  116. if (tmp && !buffer_uptodate(tmp) && !buffer_locked(tmp))
  117. bha[num++] = tmp;
  118. else
  119. brelse(tmp);
  120. }
  121. if (num) {
  122. ll_rw_block(REQ_OP_READ, REQ_RAHEAD, num, bha);
  123. for (i = 0; i < num; i++)
  124. brelse(bha[i]);
  125. }
  126. }
  127. }
  128. while (nf_pos < size) {
  129. struct kernel_lb_addr tloc;
  130. loff_t cur_pos = nf_pos;
  131. /* Update file position only if we got past the current one */
  132. if (nf_pos >= emit_pos) {
  133. ctx->pos = (nf_pos >> 2) + 1;
  134. pos_valid = true;
  135. }
  136. fi = udf_fileident_read(dir, &nf_pos, &fibh, &cfi, &epos, &eloc,
  137. &elen, &offset);
  138. if (!fi)
  139. goto out;
  140. /* Still not at offset where user asked us to read from? */
  141. if (cur_pos < emit_pos)
  142. continue;
  143. liu = le16_to_cpu(cfi.lengthOfImpUse);
  144. lfi = cfi.lengthFileIdent;
  145. if (fibh.sbh == fibh.ebh) {
  146. nameptr = fi->fileIdent + liu;
  147. } else {
  148. int poffset; /* Unpaded ending offset */
  149. poffset = fibh.soffset + sizeof(struct fileIdentDesc) + liu + lfi;
  150. if (poffset >= lfi) {
  151. nameptr = (char *)(fibh.ebh->b_data + poffset - lfi);
  152. } else {
  153. if (!copy_name) {
  154. copy_name = kmalloc(UDF_NAME_LEN,
  155. GFP_NOFS);
  156. if (!copy_name) {
  157. ret = -ENOMEM;
  158. goto out;
  159. }
  160. }
  161. nameptr = copy_name;
  162. memcpy(nameptr, fi->fileIdent + liu,
  163. lfi - poffset);
  164. memcpy(nameptr + lfi - poffset,
  165. fibh.ebh->b_data, poffset);
  166. }
  167. }
  168. if ((cfi.fileCharacteristics & FID_FILE_CHAR_DELETED) != 0) {
  169. if (!UDF_QUERY_FLAG(sb, UDF_FLAG_UNDELETE))
  170. continue;
  171. }
  172. if ((cfi.fileCharacteristics & FID_FILE_CHAR_HIDDEN) != 0) {
  173. if (!UDF_QUERY_FLAG(sb, UDF_FLAG_UNHIDE))
  174. continue;
  175. }
  176. if (cfi.fileCharacteristics & FID_FILE_CHAR_PARENT) {
  177. if (!dir_emit_dotdot(file, ctx))
  178. goto out;
  179. continue;
  180. }
  181. flen = udf_get_filename(sb, nameptr, lfi, fname, UDF_NAME_LEN);
  182. if (flen < 0)
  183. continue;
  184. tloc = lelb_to_cpu(cfi.icb.extLocation);
  185. iblock = udf_get_lb_pblock(sb, &tloc, 0);
  186. if (!dir_emit(ctx, fname, flen, iblock, DT_UNKNOWN))
  187. goto out;
  188. } /* end while */
  189. ctx->pos = (nf_pos >> 2) + 1;
  190. pos_valid = true;
  191. out:
  192. if (pos_valid)
  193. file->f_version = inode_query_iversion(dir);
  194. if (fibh.sbh != fibh.ebh)
  195. brelse(fibh.ebh);
  196. brelse(fibh.sbh);
  197. brelse(epos.bh);
  198. kfree(fname);
  199. kfree(copy_name);
  200. return ret;
  201. }
  202. /* readdir and lookup functions */
  203. const struct file_operations udf_dir_operations = {
  204. .llseek = generic_file_llseek,
  205. .read = generic_read_dir,
  206. .iterate_shared = udf_readdir,
  207. .unlocked_ioctl = udf_ioctl,
  208. .fsync = generic_file_fsync,
  209. };