dir.c 6.2 KB

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