dir.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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/smp_lock.h>
  32. #include <linux/buffer_head.h>
  33. #include "udf_i.h"
  34. #include "udf_sb.h"
  35. static int do_udf_readdir(struct inode *dir, struct file *filp,
  36. filldir_t filldir, void *dirent)
  37. {
  38. struct udf_fileident_bh fibh;
  39. struct fileIdentDesc *fi = NULL;
  40. struct fileIdentDesc cfi;
  41. int block, iblock;
  42. loff_t nf_pos = (filp->f_pos - 1) << 2;
  43. int flen;
  44. char fname[UDF_NAME_LEN];
  45. 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. kernel_lb_addr eloc;
  51. uint32_t elen;
  52. sector_t offset;
  53. int i, num;
  54. unsigned int dt_type;
  55. struct extent_position epos = { NULL, 0, {0, 0} };
  56. struct udf_inode_info *iinfo;
  57. if (nf_pos >= size)
  58. return 0;
  59. if (nf_pos == 0)
  60. nf_pos = udf_ext0_offset(dir);
  61. fibh.soffset = fibh.eoffset = nf_pos & (dir->i_sb->s_blocksize - 1);
  62. iinfo = UDF_I(dir);
  63. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
  64. fibh.sbh = fibh.ebh = NULL;
  65. } else if (inode_bmap(dir, nf_pos >> dir->i_sb->s_blocksize_bits,
  66. &epos, &eloc, &elen, &offset) == (EXT_RECORDED_ALLOCATED >> 30)) {
  67. block = udf_get_lb_pblock(dir->i_sb, eloc, offset);
  68. if ((++offset << dir->i_sb->s_blocksize_bits) < elen) {
  69. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
  70. epos.offset -= sizeof(short_ad);
  71. else if (iinfo->i_alloc_type ==
  72. ICBTAG_FLAG_AD_LONG)
  73. epos.offset -= sizeof(long_ad);
  74. } else {
  75. offset = 0;
  76. }
  77. if (!(fibh.sbh = fibh.ebh = udf_tread(dir->i_sb, block))) {
  78. brelse(epos.bh);
  79. return -EIO;
  80. }
  81. if (!(offset & ((16 >> (dir->i_sb->s_blocksize_bits - 9)) - 1))) {
  82. i = 16 >> (dir->i_sb->s_blocksize_bits - 9);
  83. if (i + offset > (elen >> dir->i_sb->s_blocksize_bits))
  84. i = (elen >> dir->i_sb->s_blocksize_bits) - offset;
  85. for (num = 0; i > 0; i--) {
  86. block = udf_get_lb_pblock(dir->i_sb, eloc, offset + i);
  87. tmp = udf_tgetblk(dir->i_sb, block);
  88. if (tmp && !buffer_uptodate(tmp) && !buffer_locked(tmp))
  89. bha[num++] = tmp;
  90. else
  91. brelse(tmp);
  92. }
  93. if (num) {
  94. ll_rw_block(READA, num, bha);
  95. for (i = 0; i < num; i++)
  96. brelse(bha[i]);
  97. }
  98. }
  99. } else {
  100. brelse(epos.bh);
  101. return -ENOENT;
  102. }
  103. while (nf_pos < size) {
  104. filp->f_pos = (nf_pos >> 2) + 1;
  105. fi = udf_fileident_read(dir, &nf_pos, &fibh, &cfi, &epos, &eloc,
  106. &elen, &offset);
  107. if (!fi) {
  108. if (fibh.sbh != fibh.ebh)
  109. brelse(fibh.ebh);
  110. brelse(fibh.sbh);
  111. brelse(epos.bh);
  112. return 0;
  113. }
  114. liu = le16_to_cpu(cfi.lengthOfImpUse);
  115. lfi = cfi.lengthFileIdent;
  116. if (fibh.sbh == fibh.ebh) {
  117. nameptr = fi->fileIdent + liu;
  118. } else {
  119. int poffset; /* Unpaded ending offset */
  120. poffset = fibh.soffset + sizeof(struct fileIdentDesc) + liu + lfi;
  121. if (poffset >= lfi) {
  122. nameptr = (char *)(fibh.ebh->b_data + poffset - lfi);
  123. } else {
  124. nameptr = fname;
  125. memcpy(nameptr, fi->fileIdent + liu,
  126. lfi - poffset);
  127. memcpy(nameptr + lfi - poffset,
  128. fibh.ebh->b_data, poffset);
  129. }
  130. }
  131. if ((cfi.fileCharacteristics & FID_FILE_CHAR_DELETED) != 0) {
  132. if (!UDF_QUERY_FLAG(dir->i_sb, UDF_FLAG_UNDELETE))
  133. continue;
  134. }
  135. if ((cfi.fileCharacteristics & FID_FILE_CHAR_HIDDEN) != 0) {
  136. if (!UDF_QUERY_FLAG(dir->i_sb, UDF_FLAG_UNHIDE))
  137. continue;
  138. }
  139. if (cfi.fileCharacteristics & FID_FILE_CHAR_PARENT) {
  140. iblock = parent_ino(filp->f_path.dentry);
  141. flen = 2;
  142. memcpy(fname, "..", flen);
  143. dt_type = DT_DIR;
  144. } else {
  145. kernel_lb_addr tloc = lelb_to_cpu(cfi.icb.extLocation);
  146. iblock = udf_get_lb_pblock(dir->i_sb, tloc, 0);
  147. flen = udf_get_filename(dir->i_sb, nameptr, fname, lfi);
  148. dt_type = DT_UNKNOWN;
  149. }
  150. if (flen) {
  151. if (filldir(dirent, fname, flen, filp->f_pos, iblock, dt_type) < 0) {
  152. if (fibh.sbh != fibh.ebh)
  153. brelse(fibh.ebh);
  154. brelse(fibh.sbh);
  155. brelse(epos.bh);
  156. return 0;
  157. }
  158. }
  159. } /* end while */
  160. filp->f_pos = (nf_pos >> 2) + 1;
  161. if (fibh.sbh != fibh.ebh)
  162. brelse(fibh.ebh);
  163. brelse(fibh.sbh);
  164. brelse(epos.bh);
  165. return 0;
  166. }
  167. /*
  168. * udf_readdir
  169. *
  170. * PURPOSE
  171. * Read a directory entry.
  172. *
  173. * DESCRIPTION
  174. * Optional - sys_getdents() will return -ENOTDIR if this routine is not
  175. * available.
  176. *
  177. * Refer to sys_getdents() in fs/readdir.c
  178. * sys_getdents() -> .
  179. *
  180. * PRE-CONDITIONS
  181. * filp Pointer to directory file.
  182. * buf Pointer to directory entry buffer.
  183. * filldir Pointer to filldir function.
  184. *
  185. * POST-CONDITIONS
  186. * <return> >=0 on success.
  187. *
  188. * HISTORY
  189. * July 1, 1997 - Andrew E. Mileski
  190. * Written, tested, and released.
  191. */
  192. static int udf_readdir(struct file *filp, void *dirent, filldir_t filldir)
  193. {
  194. struct inode *dir = filp->f_path.dentry->d_inode;
  195. int result;
  196. lock_kernel();
  197. if (filp->f_pos == 0) {
  198. if (filldir(dirent, ".", 1, filp->f_pos, dir->i_ino, DT_DIR) < 0) {
  199. unlock_kernel();
  200. return 0;
  201. }
  202. filp->f_pos++;
  203. }
  204. result = do_udf_readdir(dir, filp, filldir, dirent);
  205. unlock_kernel();
  206. return result;
  207. }
  208. /* readdir and lookup functions */
  209. const struct file_operations udf_dir_operations = {
  210. .read = generic_read_dir,
  211. .readdir = udf_readdir,
  212. .ioctl = udf_ioctl,
  213. .fsync = udf_fsync_file,
  214. };