dir.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 "udf_i.h"
  33. #include "udf_sb.h"
  34. static int udf_readdir(struct file *file, struct dir_context *ctx)
  35. {
  36. struct inode *dir = file_inode(file);
  37. struct udf_inode_info *iinfo = UDF_I(dir);
  38. struct udf_fileident_bh fibh = { .sbh = NULL, .ebh = NULL};
  39. struct fileIdentDesc *fi = NULL;
  40. struct fileIdentDesc cfi;
  41. udf_pblk_t block, iblock;
  42. loff_t nf_pos;
  43. int flen;
  44. unsigned char *fname = NULL, *copy_name = NULL;
  45. unsigned char *nameptr;
  46. uint16_t liu;
  47. uint8_t lfi;
  48. loff_t size = udf_ext0_offset(dir) + dir->i_size;
  49. struct buffer_head *tmp, *bha[16];
  50. struct kernel_lb_addr eloc;
  51. uint32_t elen;
  52. sector_t offset;
  53. int i, num, ret = 0;
  54. struct extent_position epos = { NULL, 0, {0, 0} };
  55. struct super_block *sb = dir->i_sb;
  56. if (ctx->pos == 0) {
  57. if (!dir_emit_dot(file, ctx))
  58. return 0;
  59. ctx->pos = 1;
  60. }
  61. nf_pos = (ctx->pos - 1) << 2;
  62. if (nf_pos >= size)
  63. goto out;
  64. fname = kmalloc(UDF_NAME_LEN, GFP_NOFS);
  65. if (!fname) {
  66. ret = -ENOMEM;
  67. goto out;
  68. }
  69. if (nf_pos == 0)
  70. nf_pos = udf_ext0_offset(dir);
  71. fibh.soffset = fibh.eoffset = nf_pos & (sb->s_blocksize - 1);
  72. if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
  73. if (inode_bmap(dir, nf_pos >> sb->s_blocksize_bits,
  74. &epos, &eloc, &elen, &offset)
  75. != (EXT_RECORDED_ALLOCATED >> 30)) {
  76. ret = -ENOENT;
  77. goto out;
  78. }
  79. block = udf_get_lb_pblock(sb, &eloc, offset);
  80. if ((++offset << sb->s_blocksize_bits) < elen) {
  81. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
  82. epos.offset -= sizeof(struct short_ad);
  83. else if (iinfo->i_alloc_type ==
  84. ICBTAG_FLAG_AD_LONG)
  85. epos.offset -= sizeof(struct long_ad);
  86. } else {
  87. offset = 0;
  88. }
  89. if (!(fibh.sbh = fibh.ebh = udf_tread(sb, block))) {
  90. ret = -EIO;
  91. goto out;
  92. }
  93. if (!(offset & ((16 >> (sb->s_blocksize_bits - 9)) - 1))) {
  94. i = 16 >> (sb->s_blocksize_bits - 9);
  95. if (i + offset > (elen >> sb->s_blocksize_bits))
  96. i = (elen >> sb->s_blocksize_bits) - offset;
  97. for (num = 0; i > 0; i--) {
  98. block = udf_get_lb_pblock(sb, &eloc, offset + i);
  99. tmp = udf_tgetblk(sb, block);
  100. if (tmp && !buffer_uptodate(tmp) && !buffer_locked(tmp))
  101. bha[num++] = tmp;
  102. else
  103. brelse(tmp);
  104. }
  105. if (num) {
  106. ll_rw_block(REQ_OP_READ, REQ_RAHEAD, num, bha);
  107. for (i = 0; i < num; i++)
  108. brelse(bha[i]);
  109. }
  110. }
  111. }
  112. while (nf_pos < size) {
  113. struct kernel_lb_addr tloc;
  114. ctx->pos = (nf_pos >> 2) + 1;
  115. fi = udf_fileident_read(dir, &nf_pos, &fibh, &cfi, &epos, &eloc,
  116. &elen, &offset);
  117. if (!fi)
  118. goto out;
  119. liu = le16_to_cpu(cfi.lengthOfImpUse);
  120. lfi = cfi.lengthFileIdent;
  121. if (fibh.sbh == fibh.ebh) {
  122. nameptr = fi->fileIdent + liu;
  123. } else {
  124. int poffset; /* Unpaded ending offset */
  125. poffset = fibh.soffset + sizeof(struct fileIdentDesc) + liu + lfi;
  126. if (poffset >= lfi) {
  127. nameptr = (char *)(fibh.ebh->b_data + poffset - lfi);
  128. } else {
  129. if (!copy_name) {
  130. copy_name = kmalloc(UDF_NAME_LEN,
  131. GFP_NOFS);
  132. if (!copy_name) {
  133. ret = -ENOMEM;
  134. goto out;
  135. }
  136. }
  137. nameptr = copy_name;
  138. memcpy(nameptr, fi->fileIdent + liu,
  139. lfi - poffset);
  140. memcpy(nameptr + lfi - poffset,
  141. fibh.ebh->b_data, poffset);
  142. }
  143. }
  144. if ((cfi.fileCharacteristics & FID_FILE_CHAR_DELETED) != 0) {
  145. if (!UDF_QUERY_FLAG(sb, UDF_FLAG_UNDELETE))
  146. continue;
  147. }
  148. if ((cfi.fileCharacteristics & FID_FILE_CHAR_HIDDEN) != 0) {
  149. if (!UDF_QUERY_FLAG(sb, UDF_FLAG_UNHIDE))
  150. continue;
  151. }
  152. if (cfi.fileCharacteristics & FID_FILE_CHAR_PARENT) {
  153. if (!dir_emit_dotdot(file, ctx))
  154. goto out;
  155. continue;
  156. }
  157. flen = udf_get_filename(sb, nameptr, lfi, fname, UDF_NAME_LEN);
  158. if (flen < 0)
  159. continue;
  160. tloc = lelb_to_cpu(cfi.icb.extLocation);
  161. iblock = udf_get_lb_pblock(sb, &tloc, 0);
  162. if (!dir_emit(ctx, fname, flen, iblock, DT_UNKNOWN))
  163. goto out;
  164. } /* end while */
  165. ctx->pos = (nf_pos >> 2) + 1;
  166. out:
  167. if (fibh.sbh != fibh.ebh)
  168. brelse(fibh.ebh);
  169. brelse(fibh.sbh);
  170. brelse(epos.bh);
  171. kfree(fname);
  172. kfree(copy_name);
  173. return ret;
  174. }
  175. /* readdir and lookup functions */
  176. const struct file_operations udf_dir_operations = {
  177. .llseek = generic_file_llseek,
  178. .read = generic_read_dir,
  179. .iterate_shared = udf_readdir,
  180. .unlocked_ioctl = udf_ioctl,
  181. .fsync = generic_file_fsync,
  182. };