directory.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * directory.c
  3. *
  4. * PURPOSE
  5. * Directory related functions
  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. #include "udfdecl.h"
  14. #include "udf_i.h"
  15. #include <linux/fs.h>
  16. #include <linux/string.h>
  17. #include <linux/buffer_head.h>
  18. struct fileIdentDesc *udf_fileident_read(struct inode *dir, loff_t *nf_pos,
  19. struct udf_fileident_bh *fibh,
  20. struct fileIdentDesc *cfi,
  21. struct extent_position *epos,
  22. struct kernel_lb_addr *eloc, uint32_t *elen,
  23. sector_t *offset)
  24. {
  25. struct fileIdentDesc *fi;
  26. int i, num, block;
  27. struct buffer_head *tmp, *bha[16];
  28. struct udf_inode_info *iinfo = UDF_I(dir);
  29. fibh->soffset = fibh->eoffset;
  30. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
  31. fi = udf_get_fileident(iinfo->i_ext.i_data -
  32. (iinfo->i_efe ?
  33. sizeof(struct extendedFileEntry) :
  34. sizeof(struct fileEntry)),
  35. dir->i_sb->s_blocksize,
  36. &(fibh->eoffset));
  37. if (!fi)
  38. return NULL;
  39. *nf_pos += fibh->eoffset - fibh->soffset;
  40. memcpy((uint8_t *)cfi, (uint8_t *)fi,
  41. sizeof(struct fileIdentDesc));
  42. return fi;
  43. }
  44. if (fibh->eoffset == dir->i_sb->s_blocksize) {
  45. int lextoffset = epos->offset;
  46. unsigned char blocksize_bits = dir->i_sb->s_blocksize_bits;
  47. if (udf_next_aext(dir, epos, eloc, elen, 1) !=
  48. (EXT_RECORDED_ALLOCATED >> 30))
  49. return NULL;
  50. block = udf_get_lb_pblock(dir->i_sb, eloc, *offset);
  51. (*offset)++;
  52. if ((*offset << blocksize_bits) >= *elen)
  53. *offset = 0;
  54. else
  55. epos->offset = lextoffset;
  56. brelse(fibh->sbh);
  57. fibh->sbh = fibh->ebh = udf_tread(dir->i_sb, block);
  58. if (!fibh->sbh)
  59. return NULL;
  60. fibh->soffset = fibh->eoffset = 0;
  61. if (!(*offset & ((16 >> (blocksize_bits - 9)) - 1))) {
  62. i = 16 >> (blocksize_bits - 9);
  63. if (i + *offset > (*elen >> blocksize_bits))
  64. i = (*elen >> blocksize_bits)-*offset;
  65. for (num = 0; i > 0; i--) {
  66. block = udf_get_lb_pblock(dir->i_sb, eloc,
  67. *offset + i);
  68. tmp = udf_tgetblk(dir->i_sb, block);
  69. if (tmp && !buffer_uptodate(tmp) &&
  70. !buffer_locked(tmp))
  71. bha[num++] = tmp;
  72. else
  73. brelse(tmp);
  74. }
  75. if (num) {
  76. ll_rw_block(READA, num, bha);
  77. for (i = 0; i < num; i++)
  78. brelse(bha[i]);
  79. }
  80. }
  81. } else if (fibh->sbh != fibh->ebh) {
  82. brelse(fibh->sbh);
  83. fibh->sbh = fibh->ebh;
  84. }
  85. fi = udf_get_fileident(fibh->sbh->b_data, dir->i_sb->s_blocksize,
  86. &(fibh->eoffset));
  87. if (!fi)
  88. return NULL;
  89. *nf_pos += fibh->eoffset - fibh->soffset;
  90. if (fibh->eoffset <= dir->i_sb->s_blocksize) {
  91. memcpy((uint8_t *)cfi, (uint8_t *)fi,
  92. sizeof(struct fileIdentDesc));
  93. } else if (fibh->eoffset > dir->i_sb->s_blocksize) {
  94. int lextoffset = epos->offset;
  95. if (udf_next_aext(dir, epos, eloc, elen, 1) !=
  96. (EXT_RECORDED_ALLOCATED >> 30))
  97. return NULL;
  98. block = udf_get_lb_pblock(dir->i_sb, eloc, *offset);
  99. (*offset)++;
  100. if ((*offset << dir->i_sb->s_blocksize_bits) >= *elen)
  101. *offset = 0;
  102. else
  103. epos->offset = lextoffset;
  104. fibh->soffset -= dir->i_sb->s_blocksize;
  105. fibh->eoffset -= dir->i_sb->s_blocksize;
  106. fibh->ebh = udf_tread(dir->i_sb, block);
  107. if (!fibh->ebh)
  108. return NULL;
  109. if (sizeof(struct fileIdentDesc) > -fibh->soffset) {
  110. int fi_len;
  111. memcpy((uint8_t *)cfi, (uint8_t *)fi, -fibh->soffset);
  112. memcpy((uint8_t *)cfi - fibh->soffset,
  113. fibh->ebh->b_data,
  114. sizeof(struct fileIdentDesc) + fibh->soffset);
  115. fi_len = (sizeof(struct fileIdentDesc) +
  116. cfi->lengthFileIdent +
  117. le16_to_cpu(cfi->lengthOfImpUse) + 3) & ~3;
  118. *nf_pos += fi_len - (fibh->eoffset - fibh->soffset);
  119. fibh->eoffset = fibh->soffset + fi_len;
  120. } else {
  121. memcpy((uint8_t *)cfi, (uint8_t *)fi,
  122. sizeof(struct fileIdentDesc));
  123. }
  124. }
  125. return fi;
  126. }
  127. struct fileIdentDesc *udf_get_fileident(void *buffer, int bufsize, int *offset)
  128. {
  129. struct fileIdentDesc *fi;
  130. int lengthThisIdent;
  131. uint8_t *ptr;
  132. int padlen;
  133. if ((!buffer) || (!offset)) {
  134. udf_debug("invalidparms\n, buffer=%p, offset=%p\n", buffer,
  135. offset);
  136. return NULL;
  137. }
  138. ptr = buffer;
  139. if ((*offset > 0) && (*offset < bufsize))
  140. ptr += *offset;
  141. fi = (struct fileIdentDesc *)ptr;
  142. if (fi->descTag.tagIdent != cpu_to_le16(TAG_IDENT_FID)) {
  143. udf_debug("0x%x != TAG_IDENT_FID\n",
  144. le16_to_cpu(fi->descTag.tagIdent));
  145. udf_debug("offset: %u sizeof: %lu bufsize: %u\n",
  146. *offset, (unsigned long)sizeof(struct fileIdentDesc),
  147. bufsize);
  148. return NULL;
  149. }
  150. if ((*offset + sizeof(struct fileIdentDesc)) > bufsize)
  151. lengthThisIdent = sizeof(struct fileIdentDesc);
  152. else
  153. lengthThisIdent = sizeof(struct fileIdentDesc) +
  154. fi->lengthFileIdent + le16_to_cpu(fi->lengthOfImpUse);
  155. /* we need to figure padding, too! */
  156. padlen = lengthThisIdent % UDF_NAME_PAD;
  157. if (padlen)
  158. lengthThisIdent += (UDF_NAME_PAD - padlen);
  159. *offset = *offset + lengthThisIdent;
  160. return fi;
  161. }
  162. struct short_ad *udf_get_fileshortad(uint8_t *ptr, int maxoffset, uint32_t *offset,
  163. int inc)
  164. {
  165. struct short_ad *sa;
  166. if ((!ptr) || (!offset)) {
  167. printk(KERN_ERR "udf: udf_get_fileshortad() invalidparms\n");
  168. return NULL;
  169. }
  170. if ((*offset + sizeof(struct short_ad)) > maxoffset)
  171. return NULL;
  172. else {
  173. sa = (struct short_ad *)ptr;
  174. if (sa->extLength == 0)
  175. return NULL;
  176. }
  177. if (inc)
  178. *offset += sizeof(struct short_ad);
  179. return sa;
  180. }
  181. struct long_ad *udf_get_filelongad(uint8_t *ptr, int maxoffset, uint32_t *offset, int inc)
  182. {
  183. struct long_ad *la;
  184. if ((!ptr) || (!offset)) {
  185. printk(KERN_ERR "udf: udf_get_filelongad() invalidparms\n");
  186. return NULL;
  187. }
  188. if ((*offset + sizeof(struct long_ad)) > maxoffset)
  189. return NULL;
  190. else {
  191. la = (struct long_ad *)ptr;
  192. if (la->extLength == 0)
  193. return NULL;
  194. }
  195. if (inc)
  196. *offset += sizeof(struct long_ad);
  197. return la;
  198. }