directory.c 5.8 KB

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