dir.c 4.9 KB

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