directory.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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/bio.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;
  27. udf_pblk_t block;
  28. struct buffer_head *tmp, *bha[16];
  29. struct udf_inode_info *iinfo = UDF_I(dir);
  30. fibh->soffset = fibh->eoffset;
  31. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
  32. fi = udf_get_fileident(iinfo->i_ext.i_data -
  33. (iinfo->i_efe ?
  34. sizeof(struct extendedFileEntry) :
  35. sizeof(struct fileEntry)),
  36. dir->i_sb->s_blocksize,
  37. &(fibh->eoffset));
  38. if (!fi)
  39. return NULL;
  40. *nf_pos += fibh->eoffset - fibh->soffset;
  41. memcpy((uint8_t *)cfi, (uint8_t *)fi,
  42. sizeof(struct fileIdentDesc));
  43. return fi;
  44. }
  45. if (fibh->eoffset == dir->i_sb->s_blocksize) {
  46. uint32_t lextoffset = epos->offset;
  47. unsigned char blocksize_bits = dir->i_sb->s_blocksize_bits;
  48. if (udf_next_aext(dir, epos, eloc, elen, 1) !=
  49. (EXT_RECORDED_ALLOCATED >> 30))
  50. return NULL;
  51. block = udf_get_lb_pblock(dir->i_sb, eloc, *offset);
  52. (*offset)++;
  53. if ((*offset << blocksize_bits) >= *elen)
  54. *offset = 0;
  55. else
  56. epos->offset = lextoffset;
  57. brelse(fibh->sbh);
  58. fibh->sbh = fibh->ebh = udf_tread(dir->i_sb, block);
  59. if (!fibh->sbh)
  60. return NULL;
  61. fibh->soffset = fibh->eoffset = 0;
  62. if (!(*offset & ((16 >> (blocksize_bits - 9)) - 1))) {
  63. i = 16 >> (blocksize_bits - 9);
  64. if (i + *offset > (*elen >> blocksize_bits))
  65. i = (*elen >> blocksize_bits)-*offset;
  66. for (num = 0; i > 0; i--) {
  67. block = udf_get_lb_pblock(dir->i_sb, eloc,
  68. *offset + i);
  69. tmp = udf_tgetblk(dir->i_sb, block);
  70. if (tmp && !buffer_uptodate(tmp) &&
  71. !buffer_locked(tmp))
  72. bha[num++] = tmp;
  73. else
  74. brelse(tmp);
  75. }
  76. if (num) {
  77. ll_rw_block(REQ_OP_READ, REQ_RAHEAD, num, bha);
  78. for (i = 0; i < num; i++)
  79. brelse(bha[i]);
  80. }
  81. }
  82. } else if (fibh->sbh != fibh->ebh) {
  83. brelse(fibh->sbh);
  84. fibh->sbh = fibh->ebh;
  85. }
  86. fi = udf_get_fileident(fibh->sbh->b_data, dir->i_sb->s_blocksize,
  87. &(fibh->eoffset));
  88. if (!fi)
  89. return NULL;
  90. *nf_pos += fibh->eoffset - fibh->soffset;
  91. if (fibh->eoffset <= dir->i_sb->s_blocksize) {
  92. memcpy((uint8_t *)cfi, (uint8_t *)fi,
  93. sizeof(struct fileIdentDesc));
  94. } else if (fibh->eoffset > dir->i_sb->s_blocksize) {
  95. uint32_t lextoffset = epos->offset;
  96. if (udf_next_aext(dir, epos, eloc, elen, 1) !=
  97. (EXT_RECORDED_ALLOCATED >> 30))
  98. return NULL;
  99. block = udf_get_lb_pblock(dir->i_sb, eloc, *offset);
  100. (*offset)++;
  101. if ((*offset << dir->i_sb->s_blocksize_bits) >= *elen)
  102. *offset = 0;
  103. else
  104. epos->offset = lextoffset;
  105. fibh->soffset -= dir->i_sb->s_blocksize;
  106. fibh->eoffset -= dir->i_sb->s_blocksize;
  107. fibh->ebh = udf_tread(dir->i_sb, block);
  108. if (!fibh->ebh)
  109. return NULL;
  110. if (sizeof(struct fileIdentDesc) > -fibh->soffset) {
  111. int fi_len;
  112. memcpy((uint8_t *)cfi, (uint8_t *)fi, -fibh->soffset);
  113. memcpy((uint8_t *)cfi - fibh->soffset,
  114. fibh->ebh->b_data,
  115. sizeof(struct fileIdentDesc) + fibh->soffset);
  116. fi_len = udf_dir_entry_len(cfi);
  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: %d sizeof: %lu bufsize: %d\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. }