dir.c 5.4 KB

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