dir.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. * dir.c
  3. *
  4. * PURPOSE
  5. * Directory handling routines for the OSTA-UDF(tm) filesystem.
  6. *
  7. * CONTACTS
  8. * E-mail regarding any portion of the Linux UDF file system should be
  9. * directed to the development team mailing list (run by majordomo):
  10. * linux_udf@hpesjro.fc.hp.com
  11. *
  12. * COPYRIGHT
  13. * This file is distributed under the terms of the GNU General Public
  14. * License (GPL). Copies of the GPL can be obtained from:
  15. * ftp://prep.ai.mit.edu/pub/gnu/GPL
  16. * Each contributing author retains all rights to their own work.
  17. *
  18. * (C) 1998-2004 Ben Fennema
  19. *
  20. * HISTORY
  21. *
  22. * 10/05/98 dgb Split directory operations into its own file
  23. * Implemented directory reads via do_udf_readdir
  24. * 10/06/98 Made directory operations work!
  25. * 11/17/98 Rewrote directory to support ICBTAG_FLAG_AD_LONG
  26. * 11/25/98 blf Rewrote directory handling (readdir+lookup) to support reading
  27. * across blocks.
  28. * 12/12/98 Split out the lookup code to namei.c. bulk of directory
  29. * code now in directory.c:udf_fileident_read.
  30. */
  31. #include "udfdecl.h"
  32. #include <linux/string.h>
  33. #include <linux/errno.h>
  34. #include <linux/mm.h>
  35. #include <linux/slab.h>
  36. #include <linux/smp_lock.h>
  37. #include <linux/buffer_head.h>
  38. #include "udf_i.h"
  39. #include "udf_sb.h"
  40. /* Prototypes for file operations */
  41. static int udf_readdir(struct file *, void *, filldir_t);
  42. static int do_udf_readdir(struct inode *, struct file *, filldir_t, void *);
  43. /* readdir and lookup functions */
  44. struct file_operations udf_dir_operations = {
  45. .read = generic_read_dir,
  46. .readdir = udf_readdir,
  47. .ioctl = udf_ioctl,
  48. .fsync = udf_fsync_file,
  49. };
  50. /*
  51. * udf_readdir
  52. *
  53. * PURPOSE
  54. * Read a directory entry.
  55. *
  56. * DESCRIPTION
  57. * Optional - sys_getdents() will return -ENOTDIR if this routine is not
  58. * available.
  59. *
  60. * Refer to sys_getdents() in fs/readdir.c
  61. * sys_getdents() -> .
  62. *
  63. * PRE-CONDITIONS
  64. * filp Pointer to directory file.
  65. * buf Pointer to directory entry buffer.
  66. * filldir Pointer to filldir function.
  67. *
  68. * POST-CONDITIONS
  69. * <return> >=0 on success.
  70. *
  71. * HISTORY
  72. * July 1, 1997 - Andrew E. Mileski
  73. * Written, tested, and released.
  74. */
  75. int udf_readdir(struct file *filp, void *dirent, filldir_t filldir)
  76. {
  77. struct inode *dir = filp->f_dentry->d_inode;
  78. int result;
  79. lock_kernel();
  80. if ( filp->f_pos == 0 )
  81. {
  82. if (filldir(dirent, ".", 1, filp->f_pos, dir->i_ino, DT_DIR) < 0)
  83. {
  84. unlock_kernel();
  85. return 0;
  86. }
  87. filp->f_pos ++;
  88. }
  89. result = do_udf_readdir(dir, filp, filldir, dirent);
  90. unlock_kernel();
  91. return result;
  92. }
  93. static int
  94. do_udf_readdir(struct inode * dir, struct file *filp, filldir_t filldir, void *dirent)
  95. {
  96. struct udf_fileident_bh fibh;
  97. struct fileIdentDesc *fi=NULL;
  98. struct fileIdentDesc cfi;
  99. int block, iblock;
  100. loff_t nf_pos = filp->f_pos - 1;
  101. int flen;
  102. char fname[UDF_NAME_LEN];
  103. char *nameptr;
  104. uint16_t liu;
  105. uint8_t lfi;
  106. loff_t size = (udf_ext0_offset(dir) + dir->i_size) >> 2;
  107. struct buffer_head * bh = NULL, * tmp, * bha[16];
  108. kernel_lb_addr bloc, eloc;
  109. uint32_t extoffset, elen, offset;
  110. int i, num;
  111. unsigned int dt_type;
  112. if (nf_pos >= size)
  113. return 0;
  114. if (nf_pos == 0)
  115. nf_pos = (udf_ext0_offset(dir) >> 2);
  116. fibh.soffset = fibh.eoffset = (nf_pos & ((dir->i_sb->s_blocksize - 1) >> 2)) << 2;
  117. if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
  118. fibh.sbh = fibh.ebh = NULL;
  119. else if (inode_bmap(dir, nf_pos >> (dir->i_sb->s_blocksize_bits - 2),
  120. &bloc, &extoffset, &eloc, &elen, &offset, &bh) == (EXT_RECORDED_ALLOCATED >> 30))
  121. {
  122. offset >>= dir->i_sb->s_blocksize_bits;
  123. block = udf_get_lb_pblock(dir->i_sb, eloc, offset);
  124. if ((++offset << dir->i_sb->s_blocksize_bits) < elen)
  125. {
  126. if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_SHORT)
  127. extoffset -= sizeof(short_ad);
  128. else if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_LONG)
  129. extoffset -= sizeof(long_ad);
  130. }
  131. else
  132. offset = 0;
  133. if (!(fibh.sbh = fibh.ebh = udf_tread(dir->i_sb, block)))
  134. {
  135. udf_release_data(bh);
  136. return -EIO;
  137. }
  138. if (!(offset & ((16 >> (dir->i_sb->s_blocksize_bits - 9))-1)))
  139. {
  140. i = 16 >> (dir->i_sb->s_blocksize_bits - 9);
  141. if (i+offset > (elen >> dir->i_sb->s_blocksize_bits))
  142. i = (elen >> dir->i_sb->s_blocksize_bits)-offset;
  143. for (num=0; i>0; i--)
  144. {
  145. block = udf_get_lb_pblock(dir->i_sb, eloc, offset+i);
  146. tmp = udf_tgetblk(dir->i_sb, block);
  147. if (tmp && !buffer_uptodate(tmp) && !buffer_locked(tmp))
  148. bha[num++] = tmp;
  149. else
  150. brelse(tmp);
  151. }
  152. if (num)
  153. {
  154. ll_rw_block(READA, num, bha);
  155. for (i=0; i<num; i++)
  156. brelse(bha[i]);
  157. }
  158. }
  159. }
  160. else
  161. {
  162. udf_release_data(bh);
  163. return -ENOENT;
  164. }
  165. while ( nf_pos < size )
  166. {
  167. filp->f_pos = nf_pos + 1;
  168. fi = udf_fileident_read(dir, &nf_pos, &fibh, &cfi, &bloc, &extoffset, &eloc, &elen, &offset, &bh);
  169. if (!fi)
  170. {
  171. if (fibh.sbh != fibh.ebh)
  172. udf_release_data(fibh.ebh);
  173. udf_release_data(fibh.sbh);
  174. udf_release_data(bh);
  175. return 0;
  176. }
  177. liu = le16_to_cpu(cfi.lengthOfImpUse);
  178. lfi = cfi.lengthFileIdent;
  179. if (fibh.sbh == fibh.ebh)
  180. nameptr = fi->fileIdent + liu;
  181. else
  182. {
  183. int poffset; /* Unpaded ending offset */
  184. poffset = fibh.soffset + sizeof(struct fileIdentDesc) + liu + lfi;
  185. if (poffset >= lfi)
  186. nameptr = (char *)(fibh.ebh->b_data + poffset - lfi);
  187. else
  188. {
  189. nameptr = fname;
  190. memcpy(nameptr, fi->fileIdent + liu, lfi - poffset);
  191. memcpy(nameptr + lfi - poffset, fibh.ebh->b_data, poffset);
  192. }
  193. }
  194. if ( (cfi.fileCharacteristics & FID_FILE_CHAR_DELETED) != 0 )
  195. {
  196. if ( !UDF_QUERY_FLAG(dir->i_sb, UDF_FLAG_UNDELETE) )
  197. continue;
  198. }
  199. if ( (cfi.fileCharacteristics & FID_FILE_CHAR_HIDDEN) != 0 )
  200. {
  201. if ( !UDF_QUERY_FLAG(dir->i_sb, UDF_FLAG_UNHIDE) )
  202. continue;
  203. }
  204. if ( cfi.fileCharacteristics & FID_FILE_CHAR_PARENT )
  205. {
  206. iblock = parent_ino(filp->f_dentry);
  207. flen = 2;
  208. memcpy(fname, "..", flen);
  209. dt_type = DT_DIR;
  210. }
  211. else
  212. {
  213. kernel_lb_addr tloc = lelb_to_cpu(cfi.icb.extLocation);
  214. iblock = udf_get_lb_pblock(dir->i_sb, tloc, 0);
  215. flen = udf_get_filename(dir->i_sb, nameptr, fname, lfi);
  216. dt_type = DT_UNKNOWN;
  217. }
  218. if (flen)
  219. {
  220. if (filldir(dirent, fname, flen, filp->f_pos, iblock, dt_type) < 0)
  221. {
  222. if (fibh.sbh != fibh.ebh)
  223. udf_release_data(fibh.ebh);
  224. udf_release_data(fibh.sbh);
  225. udf_release_data(bh);
  226. return 0;
  227. }
  228. }
  229. } /* end while */
  230. filp->f_pos = nf_pos + 1;
  231. if (fibh.sbh != fibh.ebh)
  232. udf_release_data(fibh.ebh);
  233. udf_release_data(fibh.sbh);
  234. udf_release_data(bh);
  235. return 0;
  236. }